私はWPFを初めて使用します。現在、MSVC#2010のソリューションに2つのプロジェクトがあるアプリケーションを開発しています。1つのプロジェクト(ieMSPBoardControl)にmainwindow.xamlがあり、別のwpfウィンドウConnectView.xamlを追加しました。mainwindow.xamlのグリッドを使用することで、Connectview.xamlビューをmainwindow.xaml.csのアプリケーションに追加できます。私の2番目のプロジェクトは、usercontrol(つまりVoltage)を持つクラスライブラリです。
Mainwindow.xaml:このグリッドは、Connectview.xamlUIコンポーネントを追加するグリッドです。
<Grid Grid.Row="0" Name="MainGrid" HorizontalAlignment="Stretch" Style="{DynamicResource styleBackground}" >
</Grid>
mainwindow.xamlの左側に、アイテム(電圧、時計など)のリストがあるリストボックスがあります。.xamlの右側には、上に示したグリッドがあります(起動時のconnectviewが含まれています)。基本的に必要なのは、リストボックスからアイテムをクリックしたときに、startup(connectview)に表示されるビューを非表示にして、選択したアイテムのUIコンポーネントを表示することです。
冒頭で述べたように、リストボックスから「Voltage」アイテムをクリックするときに、クラスライブラリ(VoltageView)のユーザーコントロールを表示したいと思います。
mainwindow.xamlのListBox:
<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()は、選択されたアイテムを取得し、条件dat item = Voltageをチェックするときに、電圧ビューコンポーネントをメイングリッドに追加し、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>
助けてください!!!