简体   繁体   中英

WPF MVVM : Bind a command which is on a ViewModel in a Child ItemSource

Im using WPF with MVVM design-patern, and I need to bind a command which is of a ItemsSource binding parent : View :

<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";
    }
}

I want to make a bind on my Checkbox object : How can I do this while the Items source of the HierarchicalDataTemplate is set to "Tasks" I can only binding on my "Tasks" filds. How can get the parent of the ItemSource ?

instead of finding the right parent and view model...I had a Forward pattern mainly for context menu...

All my view model have this command

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;
        }
    }

it is then forwarded to the view model that have registered to a forward_message

Messenger.Default.Register<CommandContext>(this, ViewModelBaseMessages.ContextCommand,
            (CommandContext o) =>
            {
                ExecuteDistantCommand( o );
            } );

you can find this code in my project on github please, use a mvvm nuget like mvvmlight and do not invent the wheel...;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM