ComboBox
は、メインビューモデルによって公開されたフレームアイテムのObservableCollection
を表示し、ビューモデルは選択したアイテムに対して別のプロパティを持ちます。
メインビューモデルとフレームアイテムビューモデルはすべて、INotifyPropertyChanged
を実装するViewModelBase
クラスから継承しています。
ので、C#の:
public ObservableCollection<ViewModelBase> FrameItems { get; protected set; }
private ViewModelBase _selectedFrameItem;
public ViewModelBase SelectedFrameItem {
get { return _selectedFrameItem; }
set {
value = _selectedFrameItem;
// Defined in ViewModelBase
OnPropertyChanged();
}
}
あなたのメインのviewmodelは、コンストラクタでFrameItems
を移入します:
public MainViewModel()
{
FrameItems = new ObservableCollection<ViewModelbase> {
new IceCreamMenu(),
new SmurfOptions(),
new MagicSparklePonyFourierTransformConfiguration()
};
}
すべてのフレーム項目はViewModelBase
のサブクラスです。それは、子のもののセットのObservableCollections
を含む通知でプロパティを公開します。そして、ちょうどそれのためのデータ型を書くことでそれを表示します。
ViewModelBase
クラスにString Title { get; set; }
プロパティを指定したとします。あるいは、ViewModelBase
のサブクラスを作成して、Title
を導入することもできます。あなたの電話。ここでは簡潔にするためにViewModelBase
に入れてみましょう。
XAML - これはすべてのレイアウトを省略しますが、ここでは必要ありません。
<ComboBox
ItemsSource="{Binding FrameItems}"
SelectedItem="{Binding SelectedFrameItem}"
DisplayMemberPath="Title"
/>
<Frame Content={Binding SelectedFrameItem}" />
でも、SelectedFrameItem
とどうすればいいですか?
Easy!たとえばViewModelDataTemplates.xaml
というリソースディクショナリを作成し、App.xaml
にマージして、その内容がアプリケーションのすべてのXAMLで「可視」になるようにします。
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Source is a relative path from project root directory -->
<ResourceDictionary Source="ViewModelDataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
...プラスどんなテーマのものまたは何でも。
ViewModelDataTemplates.xaml
には、フレームアイテムクラスのデータテンプレートを定義します。
あなたはFlavors
public ObservableCollection<IceCreamFlavor> Flavors { get; protected set; }
のコレクション...とSelectedFlavor
で、IceCreamMenu
のviewmodelを持っていると言います。リソース辞書のxmlns:vm
属性で適切に名前空間vm
を定義します。
ViewModelDataTemplates.xaml
<DataTemplate DataType="{x:Type vm:IceCreamMenu}">
<Grid>
<ListBox
ItemsSource="{Binding Flavors}"
SelectedItem="{Binding SelectedFlavor}"
/>
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:IceCreamFlavor}">
<StackPanel Orientation="Horizontal">
<Border
Height="20"
Width="20"
Margin="4"
Background={Binding Color, Converter={StaticResource ColorToBrushConverter}}"
/>
<Label Content="Name" />
</StackPanel>
</DataTemplate>
あなたがdatatemplates経由で使用したいUserControls
を既存持っていれば、それは簡単です:あなたはNotesTabViewModel
ためのビューですNotesTabView
UserControl
を持っていると言う、あなたが定義することができこのようなDataTemplate:
<DataTemplate DataType="{x:Type vm:NotesTabViewModel}">
<vw:NotesTabView />
</DataTemplate>
これを行うと、コンボボックスから項目を選択し、インスタンス化するビューモデルの名前だけにフレームの内容を変更します。あたかも.tostring()メソッドを実行するかのようになります。 DataTemplate全体で、データの正しい表示方法が定義されていますか? 既に作成されているページごとに別々のビューを作成しているので、正しいビューに移動する必要があります。 – Tbooty
フレームに配置したビューモデルタイプのデータテンプレートが見つからないようです。 App.xamlでdatatemplates.xamlをマージしていますか? –
@Tbooty私の回答 –