gpt4 book ai didi

c# - WPF Caliburn Micro Message.Attach 不通过 HierarchicalDataTemplate 冒泡

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:43 28 4
gpt4 key购买 nike

我正在尝试连接 TreeView 中的删除按钮,以便它在其直接父 View 模型中调用 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>

这是我的 View 模型:

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

最佳答案

您可以使用 cal:Action.TargetWithoutContext 附加属性将 ButtonDataContext 设置为父 B_ViewModel:

<DataTemplate DataType="{x:Type vm:C_ViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CName}" />
<Button Margin="2"
cal:Action.TargetWithoutContext="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=TreeViewItem, AncestorLevel=2}}"
cal:Message.Attach="Remove($dataContext)"
Content="Delete"/>
</StackPanel>
</DataTemplate>

关于c# - WPF Caliburn Micro Message.Attach 不通过 HierarchicalDataTemplate 冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55104978/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com