我使用WPF和MVVM design-patern,我需要绑定一个ItemsSource绑定父命令: 查看:
<Window x:Class="TaskSupervisor.MainWindow"
...
...>
<Window.DataContext>
<local:TacheGroupeViewModel/>
</Window.DataContext>
<Grid>
<TreeView x:Name="TachesTree" ItemsSource="{Binding TachesGroupes.GroupesConfig}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Value.Tasks}">
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Grid>
<CheckBox x:Name="InProgress"
IsChecked="{Binding ModifInProgress}">
</CheckBox>
<StackPanel Grid.Row="2" Grid.Column="2" Orientation="Horizontal">
...
</StackPanel>
<StackPanel Grid.Row="3" Grid.Column="2" Orientation="Horizontal">
...
</StackPanel>
</Grid>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
<TextBlock Text="{Binding Value.DisplayGroupe, Mode=OneWay}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
ViewModel:
class TacheGroupeViewModel : INotifyPropertyChanged {
public Model.TachesGroupes TachesGroupes { get; set; }
public TacheGroupeViewModel() {
this.TachesGroupes = new Model.TachesGroupes();
// return a Dictionary<string, CustomClass> Object
// CustomClass => Contain some field and a List Of custom class
}
private ICommand _cmdEnCoursDeTraitement;
public ICommand CmdEnCoursDeTraitement {
get {
return _cmdEnCoursDeTraitement ?? ( _cmdEnCoursDeTraitement = new RelayCommand(x => {Test(); }) );
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void Test() {
var test = "xD";
}
}
我想在我的Checkbox对象上进行绑定:如何在HierarchicalDataTemplate的Items源设置为&#34; Tasks&#34;我只能约束我的&#34;任务&#34; filds。 如何获得ItemSource的父级?
答案 0 :(得分:0)
而不是找到正确的父级和视图模型......我主要为上下文菜单设置了前向模式......
我的所有视图模型都有此命令
private ICommand forwardCommand;
public ICommand ForwardCommand
{
get
{
if (forwardCommand == null)
forwardCommand = new RelayCommand<string>(
delegate(string param)
{
Messenger.Default.Send<CommandContext>(GetForwardCommandContext(param),
ViewModelBaseMessages.ContextCommand );
},
delegate(string param)
{
return CanForwardCommand(param);
});
return forwardCommand;
}
}
然后将其转发到已注册到forward_message
的视图模型Messenger.Default.Register<CommandContext>(this, ViewModelBaseMessages.ContextCommand,
(CommandContext o) =>
{
ExecuteDistantCommand( o );
} );
您可以在github的项目中找到此代码 请使用像mvvmlight这样的mvvm nuget,不要发明轮子......; - )