私はここにすべての私の問題は、データが(私は長年のWinFormsを使用)バインディングについての理解不足に属していると思うので、私は本当にXAMLおよびWPFについての本を読み始める必要があります知っている:DataGridRowのContextMenuItemとDataGridRow自体の間のデータバインディングは、カプセル化されたViewModel内のプロパティに対してどのように機能しますか?
私のアプリケーションは、ツリービューで構成され、 DataGrid。
TreeViewでは、各ParentNode、ViewNod、GrandChildNodeにViewModelsを追加しました。
私はジョシュ・スミスのサンプルを使用しましたfound here。
は短いことが、彼/私はChildNodeViewModelに、対応するモデルに子ノードをバインドする
<HierarchicalDataTemplate
DataType="{x:Type local:ViewModel.TreeViewChildNodeViewModel}"
ItemsSource="{Binding Children}">
</HierarchicalDataTemplate.Resources>
を使用。
追加よりも、I - TreeViewChildNodeViewModelコンストラクタで:
このプロパティを見るにさらされているContextMenuItems = new List<MenuItem>();
ContextMenuItems.Add(new MenuItem() {
Header = "Click",
Command = _cmdDoSmth
ToolTip = new ToolTip() { Content = "blabla" }
}
);
:私は複数のコンストラクタを持っている、ということ
private readonly List<MenuItem> ContextMenuItems;
public List<MenuItem> ContextMenu {
get { return ContextMenuItems; }
}
注意。どのモデルでViewModelを操作するかによって、ContextMenuItemsをContextMenuリストに追加します。 「ルート」ChildNodeは、次のように構成されています。
<TextBlock
Text="{Binding ChildNodeDisplayItem}">
<TextBlock.ContextMenu>
<ContextMenu
ItemsSource="{Binding ContextMenu}"></ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
これは適切に動作します。今私の問題は、データグリッドと似たようなことをしようとすることから始まります。私は達成するために必要なもの
は次のとおりです。
私はデータグリッド内の行を表示したいと思います。各行には独自のViewModelがあり、ContextMenuItemのリスト(もちろんモデル)も公開されています。私は、選択されたビューモデルに依存して、各コンテキストメニューの数、ヘッダ、およびコマンドを定義できるようにしたいと思います。
public MainWindowViewModel() // Constructor
{
actionReactionDataGrid = new ObservableCollection<ActionReactionDataGridViewModel>();
actionReactionDataGrid.Add(new ActionReactionDataGridViewModel());
}
private ObservableCollection<ActionReactionDataGridViewModel> actionReactionDataGrid;
public ObservableCollection<ActionReactionDataGridViewModel> ActionReactionDataGridViewModel
{
get { return actionReactionDataGrid; }
}
マイActionReactionDataGridViewModelはここにある:
public class ActionReactionDataGridViewModel : ViewModelBase
{
private readonly List<MenuItem> ContextMenuItems;
public ActionReactionDataGridViewModel()
{
ContextMenuItems = new List<MenuItem>();
ContextMenuItems.Add(new MenuItem()
{
Header = "blubb"
});
dataGridSource = new ObservableCollection<ActionReactionDataGridModel>();
dataGridSource.Add(new ActionReactionDataGridModel("Status","Eventname","Eventtyp","ReaktionName","ReaktionTyp"));
}
public List<MenuItem> ContextMenu {
get { return ContextMenuItems; }
}
private ActionReactionDataGridModel selectedDataGridItem;
public ActionReactionDataGridModel SelectedDataGridItem {
get { return selectedDataGridItem; }
set {selectedDataGridItem = value; RaisePropertyChanged("SelectedDataGridItem"); }
}
private ObservableCollection<ActionReactionDataGridModel> dataGridSource;
public ObservableCollection<ActionReactionDataGridModel> DataGridSource {
get { return dataGridSource; }
set { dataGridSource = value; RaisePropertyChanged("DataGridSource"); }
}
}
私MainWindow.xamlに:私のMainWindowViewModelで
<Controls:MetroWindow.Resources>
<ContextMenu x:Key="DataRowContextMenu" ItemsSource="{Binding Path=ActionReactionDataGridViewModel/ContextMenu, RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor}}"/>
</Controls:MetroWindow.Resources>
<DataGrid
AutoGenerateColumns="True"
AutoGeneratingColumn="OnAutoGeneratingColumn"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
BorderThickness="1,1,1,1"
Margin="0,0,0,0"
ItemsSource="{Binding Path=ActionReactionDataGridViewModel/DataGridSource}"
SelectedItem="{Binding Path=ActionReactionDataGridViewModel/SelectedDataGridItem}"
BorderBrush="#FF020202">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" /> </Style>
<DataGrid.RowStyle>
</DataGrid>
私がこれまでにやった
私 モデルのコンテンツを投稿することは、列ヘッダーといくつかのサンプル文字列を含んでいるだけなので、必要ではないと考えています。私は、「ActionReactionDataGridViewModel」の代わりに「DataGridSource」にitemssourceをバインドするために、MainWindow.xamlのビューでDataGridコントロールにDataGridコントロールに通知する知識がないと思います。
私は、コンテキストメニューをデータグリッドに追加することについて、他の投稿を見つけました。私が欠けていたのは、カウント、テキスト、およびコマンドを各ビューモデルにバインドする機能です。
ご迷惑をおかけして申し訳ございません。
ありがとうございます。
// EDIT 1
[OK]をクリックします。ビューモデルのコレクションの内部からビューモデルのプロパティを渡す方法を見つけることは簡単でした。
私は説明は、メニュー内のhere
今、私は "ただ" ...それぞれのviewmodelにコンテキストメニュー項目を追加する方法を把握する必要があり
doenstこれは、コントロール内のどこにでもコンテキストメニューを表示させる原因になりますか?私はそれが "ActionReactionDataGridViewModel.ContextMenu"と全体の行にバウンドすることを望みます。 – c3rebro
内部のDataGridが表示される場所 –