WPF Caliburn Micro Message.Attach无法通过HierarchicalDataTemplate冒泡

时间:2019-03-11 15:13:05

标签: c# wpf mvvm caliburn.micro hierarchicaldatatemplate

我试图在TreeView中连接删除按钮,以便它在其直接父视图模型中调用Remove方法。

enter image description here

当我单击“删除”按钮时,出现以下异常:System.Exception: 'No target found for method Remove.'

如果我将Remove方法从B_ViewModel移到SomeViewModel,则会被调用,但是我不希望那样实现。

我如何告诉Message.Attach冒充其直接父母?

这里是SomeView.xaml

<TreeView ItemsSource="{Binding As}">
<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type vm:A_ViewModel}"
                                ItemsSource="{Binding Bs}">
        <TextBlock Text="{Binding AName}"  />
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type vm:B_ViewModel}"
                                ItemsSource="{Binding Cs}">
        <TextBlock Text="{Binding BName}"  />
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type vm:C_ViewModel}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding CName}"  />
            <Button Margin="2" 
                        cal:Message.Attach="Remove($dataContext)" 
                    Content="Delete"/>
        </StackPanel>
    </DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
        <Setter Property="IsExpanded" Value="True" />
    </Style>
</TreeView.ItemContainerStyle>

这是我的视图模型:

public class SomeViewModel: Screen
{
    public BindableCollection<A_ViewModel> As { get; private set; } 
        = new BindableCollection<A_ViewModel>();
    public SomeViewModel()
    {
        var a = new A_ViewModel() { AName = "A1" };
        var b = new B_ViewModel() { BName = "B1" };
        b.Cs.Add(new C_ViewModel() { CName = "C1" });
        b.Cs.Add(new C_ViewModel() { CName = "C2" });
        a.Bs.Add(b);
        this.As.Add(a);
    }
}
public class A_ViewModel: PropertyChangedBase
{
    public BindableCollection<B_ViewModel> Bs { get; private set; } 
        = new BindableCollection<B_ViewModel>();
    public A_ViewModel()
    {
    }
    private string _aName;

    public string AName
    {
        get { return _aName; }
        set { Set(ref _aName, value); }
    }
}
public class B_ViewModel : PropertyChangedBase
{
    public BindableCollection<C_ViewModel> Cs { get; private set; } 
        = new BindableCollection<C_ViewModel>();
    private string _bName;
    public string BName
    {
        get { return _bName; }
        set { Set(ref _bName, value); }
    }
    public void Remove(C_ViewModel c)
    {
        if (this.Cs.Contains(c))
            this.Cs.Remove(c);
        else
            Debug.Print(c.CName + " not found");
    }
}
public class C_ViewModel : PropertyChangedBase
{
    private string _cName;
    public string CName
    {
        get { return _cName; }
        set { Set(ref _cName, value); }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用附加的cal:Action.TargetWithoutContext属性将DataContext的{​​{1}}设置为父Button

B_ViewModel