我正在开发 MVVM 示例应用程序。我在 VSC# 2010 的解决方案中有 2 个项目。在一个项目(主项目)中,我有一个 mainwindow.xaml,它有两个网格,一个位于右侧,另一个位于左侧。左侧网格有一个列表框,它在 observablecollection<> 的帮助下包含项目。例如电压、I2C 等是项目。我还设置了selecteditem
列表框的属性,它给了我selecteditem
.
我的主要查询是右侧的网格,它有一个 TABCONTROL,默认添加一个项目(“CONNECT”)。当我从 listBox 中选择“Voltage”项时,我需要在 tabcontrol 中添加 tabitem(例如电压选项卡)。
我的网格中的 TabControl 和 ListBox:
<Grid Grid.Column="0" Name="BoardTabSelect" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ListBox Name="ButtonPanel" Style="{DynamicResource styleBanner}" ItemsSource="{Binding BoardTabs, Mode=TwoWay}" SelectedItem="{Binding SelectedTab, Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="BoardtabChanger" Margin="53,27,0,0" Text="{Binding TabOperation}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
我的选项卡控件:
<Grid Grid.Row="0" >
<TabControl Name="ConnectTab" Style="{DynamicResource styleBackground}">
<tablocal:CloseableTabItem Header="Connect" x:Name="ConnectMain" MouseDoubleClick="TabItem_MouseDoubleClick">
<DockPanel>
<ListView Name="listView" Height="460" Margin="0,-77,0,0" ItemsSource="{Binding Products}" SelectedItem="{Binding SelectedProduct, Mode=TwoWay}">
<ListView.View>
<GridView>
<GridViewColumn Width="300" Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="283" Header="Connection Status" DisplayMemberBinding="{Binding Connection_Status}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="Connect" Height="23" HorizontalAlignment="Stretch" Margin="-920,500,0,0" Name="ConnectBtnGrid" VerticalAlignment="Stretch" Width="100" Command="{Binding Path=ConnectCommand}" />
<Button Content="Update MSP430" Height="23" HorizontalAlignment="Stretch" Margin="-560,500,0,0" Name="UpdateMSPBtn" VerticalAlignment="Stretch" Width="100" />
<Button Content="Disconnect" Height="23" HorizontalAlignment="Stretch" Margin="-220,500,0,0" Name="DisconnectBtn" VerticalAlignment="Stretch" Width="100" Command="{Binding Path=DisconnectCommand}" />
</DockPanel>
</tablocal:CloseableTabItem>
</TabControl>
</Grid>
我的 ViewModel 类在这里:
public List<Product> m_Products;
public ObservableCollection<Product> m_BoardTabs;
public ProductViewModel()
{
m_Products = new List<Product>()
{
new Product() {Name = "Bavaria", Connection_Status = "Disconnected"},
new Product() {Name = "Redhook", Connection_Status = "Disconnected"},
};
m_BoardTabs = new ObservableCollection<Product>()
{
new Product() {TabOperation = "Connect"}
};
}
public List<Product> Products
{
get
{
return m_Products;
}
set
{
m_Products = value;
}
}
public ObservableCollection<Product> BoardTabs
{
get
{
return m_BoardTabs;
}
set
{
m_BoardTabs = value;
}
}
/// <summary>
/// get:
/// set:
/// </summary>
private Product m_SelectedItem;
public Product SelectedProduct
{
get
{
return m_SelectedItem;
}
set
{
m_SelectedItem = value;
NotifyPropertyChanged("SelectedProduct");
}
}
private Product m_SelectedTab;
public Product SelectedTab
{
get
{
return m_SelectedTab;
}
set
{
m_SelectedTab = value;
NotifyPropertyChanged("SelectedTab");
onTabChanged();
}
}
void onTabChanged()
{
if (SelectedTab.TabOperation == "Voltage")
{
//Add tab here
}
}
public Product value { get; set; }
/// <summary>
/// get:
/// set:
/// </summary>
private ICommand mUpdater;
public ICommand ConnectCommand
{
get
{
if (mUpdater == null)
mUpdater = new DelegateCommand(new Action(SaveExecuted), new Func<bool>(SaveCanExecute));
return mUpdater;
}
set
{
mUpdater = value;
}
}
public bool SaveCanExecute()
{
return true;
}
public void SaveExecuted()
{
if (SelectedProduct.Connection_Status == "Disconnected" && SelectedProduct.Name == "Bavaria")
{
SelectedProduct.Connection_Status = "Connected";
m_BoardTabs.Add(new Product() { TabOperation = "I2C" });
m_BoardTabs.Add(new Product() { TabOperation = "Voltage" });
m_BoardTabs.Add(new Product() { TabOperation = "Clock" });
m_BoardTabs.Add(new Product() { TabOperation = "Codec" });
m_BoardTabs.Add(new Product() { TabOperation = "EEPROM" });
}
else if (SelectedProduct.Connection_Status == "Disconnected" && SelectedProduct.Name == "Redhook")
{
SelectedProduct.Connection_Status = "Connected";
m_BoardTabs.Add(new Product() { TabOperation = "I2C" });
m_BoardTabs.Add(new Product() { TabOperation = "Voltage" });
m_BoardTabs.Add(new Product() { TabOperation = "Clock" });
m_BoardTabs.Add(new Product() { TabOperation = "Codec" });
m_BoardTabs.Add(new Product() { TabOperation = "EEPROM" });
m_BoardTabs.Add(new Product() { TabOperation = "PCM Route" });
m_BoardTabs.Add(new Product() { TabOperation = "PCM Route #" });
m_BoardTabs.Add(new Product() { TabOperation = "PCM Gen" });
m_BoardTabs.Add(new Product() { TabOperation = "SD Card" });
m_BoardTabs.Add(new Product() { TabOperation = "FPGA" });
m_BoardTabs.Add(new Product() { TabOperation = "PCMPDM" });
m_BoardTabs.Add(new Product() { TabOperation = "Data Gen" });
}
}
请注意,我想在控件中添加的 tabitem “VOLTAGE”是我在开头提到的另一个项目的一部分。它有自己的视图、视图模型和模型类。以下是我要添加的视图 in.xaml 文件:
<Grid Name="VoltageTab" Height="572" Width="590" DataContext="{StaticResource VoltageViewModel}" >
// Some UI componments which shud be placed inside VOLTAGE Tab on selecting Voltage from ListBox.
</Grid>
正如您在上面看到的,我希望将上面的视图放置在电压选项卡中,一旦用户从列表框中单击电压项,该选项卡就会创建。