How to connect button in Silverlight ListItem DataTemplate in ResourceDictionary (Styles.xaml) using handler?

OK, so I am defining an ItemTemplate for the ListBox in the ResourceDictionary (Styles.xaml). The ListBoxItem template looks something like this:

<ControlTemplate TargetType="ListBoxItem">
    <Button Command="{Binding Path=DoSomeCommand}" Content="Test"  />
</ControlTemplate>

Now that this template is in use, I would like this button to click the snap button to the available ViewModel command to process it.

However, this does not work as it is, I also tried this:

<ControlTemplate TargetType="ListBoxItem">
    <Button Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DoSomeCommand}" Content="Test"  />
</ControlTemplate>

But still no dice.

A simple example that works if you define a template in the control (resources) that uses it and just use an event handler (the same handler for all generated XAMLs.

? , : , ListBox.

!

+3
2

, :

'' ViewModels :

, , ListBox DTO/business ( ), ViewModel DTO , VM .

, :

    TestItems = new ObservableCollection<ItemVM> ()
    {
        new ItemVM(),
        new ItemVM(),
        new ItemVM()
    };

ItemVM DTO :

public class ItemVM : INotifyPropertyChanged
{
    public ItemVM ()
    {
        this.MyCommand = new DelegateCommand<string> ( TheCommand );
    }

    public ICommand MyCommand { get; private set; } 
    public MyBusinessObject BizObj;
}

voila, RelativeSource, .

+1

: DataTemplate

: Prism Blend Behaviors.

0

Source: https://habr.com/ru/post/1729582/


All Articles