未能将UserControl添加到ListBox中SelectedItem的现有网格中



我是WPF的新手。我目前正在开发一个应用程序,我在MSVC#2010中的解决方案有两个项目。一个项目(即MSPBoardControl)有一个mainwindow.xaml,我添加了另一个wpf窗口ConnectView.xaml。通过使用mainwindow.xaml中的网格,我可以将ConnectView.xaml视图添加到mainwindow.xaml.cs中的应用程序中。我的第二个项目是一个类库,它有一个用户控件(即电压)。

Mainwindow.xaml:这个网格是我添加Connectview.xaml UI组件的网格

<Grid Grid.Row="0" Name="MainGrid" HorizontalAlignment="Stretch" Style="{DynamicResource styleBackground}" >                        
</Grid>

我在我的主窗口.xaml左侧有一个列表框,其中有项目列表(即电压、时钟等)。xaml的右侧有我的网格,如上图所示(包含启动时的connectview)。我基本上需要的是,当我单击列表框中的项目时,我想隐藏启动时显示的视图(connectview),并使selecteditem UI组件可见。

正如开头所提到的,我想让我的类库(VoltageView)的用户控件在列表框中的"Voltage"项上可见。

我的主窗口中的ListBox.xaml:

<ListBox Name="ButtonPanel" Style="{DynamicResource styleBanner}" ItemsSource="{Binding BoardOperations, Mode=TwoWay}" SelectedItem="{Binding SelectedList}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Name="BoardTabChanger" Margin="53,27,0,0" Text="{Binding ListName}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

MSPBoardControl的ViewModel类在这里:

public class BoardControlViewModel : INotifyPropertyChanged
{        
public ObservableCollection<BoardControlModel> m_BoardOperation;
public List<ConnectModel> m_BoardNames; 
public BoardControlViewModel()
{
//Adding items to ListBox
}
public List<ConnectModel> BoardNames
{
get; set;
}
private ConnectModel m_SelectedBoardItem;
public ConnectModel SelectedBoard
{
get; set;
}
public ObservableCollection<BoardControlModel> BoardOperations
{
get; set;
}
//GIVES ME THE SELECTED ITEM FROM LISTBOX
private BoardControlModel m_SelectedListItem;
public BoardControlModel SelectedList
{
get
{
return m_SelectedListItem;
}
set
{
m_SelectedListItem = value;
onListSelected(value);
NotifyPropertyChanged("SelectedList");                
}
}
private void onListSelected(BoardControlModel SelectedItem)
{
if (SelectedItem.ListName == "Voltage")
{
//HOW CAN I ADD THE VOLTAGEVIEW HERE???
}
}

上面的方法OnListSelected()检索selectedem,当我检查条件dat-item=voltage时,我想将voltageview组件添加到我的主电网中并隐藏connectview。

VoltageView.xaml:

<Grid Name="VoltageControl" Style="{DynamicResource styleBackground}" DataContext="{StaticResource VoltageViewModel}" >
<Label Content="Label" FontSize="15" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="10,31,0,0" Name="label9" VerticalAlignment="Top" Width="117" />
<Label Content="Set Voltage" FontSize="15" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="150,31,0,0" Name="label10" VerticalAlignment="Top" Width="117" />
<Label Content="Current Value" FontSize="16" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="300,31,0,0" Name="label11" VerticalAlignment="Top" Width="117" />
<Label Content="Enable/Disable" FontSize="15" Foreground="Black" Height="28" HorizontalAlignment="Left" Margin="460,31,0,0" Name="label12" VerticalAlignment="Top" Width="127" />
<Button Content="Refresh All" FontSize="13" Height="25" HorizontalAlignment="Left" Margin="230,520,0,0" Name="RefreshBtn" VerticalAlignment="Top" Width="105" />
</Grid>    

请帮忙!!!

我将保留这个通用(ish)。你最好的选择可能是在BoardControlViewModel上添加一个类型为Grid的属性(或电压和连接通用的任何UIElement)。随着选择的更改,请将UI属性更改为所需的视图,然后为该属性单击NotifyPropertyChanged。如果您愿意,可以从onListSelected调用NotifyPropertyChanged权限。将您使用的任何网格的内容绑定到此属性,最好是在xaml中。当属性更改时,WPF将通知网格其绑定内容已更改,并查询您的新属性以查看它应该是什么。

最新更新