gpt4 book ai didi

c# - 如何从 subview 模型绑定(bind)到父 View 模型中的事件

转载 作者:行者123 更新时间:2023-12-03 10:35:55 24 4
gpt4 key购买 nike

我目前关注 this使用复选框设置 TreeView 的指南。在我的代码中,树“FooViewModel”在我的 MainViewModel 中启动,并作为 ItemsSource 绑定(bind)到 TreeView。我希望能够订阅 MainViewModel 中的某些事件,该事件将在选中或取消选中某些内容时触发。这样我就可以遍历“FooViewModel”并检查哪些节点具有 IsChecked = True。如何创建此事件绑定(bind)?

这是我的代码:

<Style x:Key="TreeViewItemStyle" TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="False" />
<Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}" />
<Setter Property="KeyboardNavigation.AcceptsReturn" Value="True" />
<Setter Property="xn:VirtualToggleButton.IsVirtualToggleButton" Value="True" />
<Setter Property="xn:VirtualToggleButton.IsChecked" Value="{Binding IsChecked}" />
<Setter Property="Focusable" Value="False" />
</Style>

<xn:TreeView ItemsSource="{Binding CollectionFooViewModel}" ItemContainerStyle="{StaticResource TreeViewItemStyle}">
<xn:TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children, Mode=OneTime}">
<StackPanel Orientation="Horizontal">
<CheckBox Focusable="False" IsChecked="{Binding IsChecked}" VerticalAlignment="Center"/>
<ContentPresenter Content="{Binding Name, Mode=OneTime}" Margin="2,0"/>
</StackPanel>
</HierarchicalDataTemplate>
</xn:TreeView.ItemTemplate>
</xn:TreeView>

我想如果有办法将“IsChecked”绑定(bind)到两个属性(一个在 FooViewModel 中,另一个在 MainViewModel 中),我会得到答案。

最佳答案

有很多方法可以实现这一目标。一种是某种发布/订阅(消息)实现,或者可能只是一堆 Action代表?就像是...

主窗口

<Window x:Class="WpfApplication1.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="300"
Width="250">

<TreeView ItemsSource="{Binding CollectionFooViewModel}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children, Mode=OneTime}">
<StackPanel Orientation="Horizontal">
<CheckBox Focusable="False"
IsChecked="{Binding IsChecked}"
VerticalAlignment="Center"/>
<ContentPresenter Content="{Binding Name, Mode=OneTime}"
Margin="2,0"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Window>

数据上下文
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Action<MyItem> action = item => Console.WriteLine(@"MyItem was clicked");

CollectionFooViewModel = new ObservableCollection<MyItem>()
{
new MyItem()
{
Name = "MyItem1",
Children = new List<MyItem>()
{
new MyItem()
{
Name = "MySubItem1",
IsChecked = false,
Action = item => Console.WriteLine(@"{0} invoked action", item.Name)
},
new MyItem()
{
Name = "MySubItem2",
IsChecked = true,
Action = item => Console.WriteLine(@"{0} state is {1} ", item.Name, item.IsChecked)
},
},
Action = action
}
};
}

public ObservableCollection<MyItem> CollectionFooViewModel { get; set; }
}

public class MyItem : ViewModelBase
{
private bool _isChecked;
public string Name { get; set; }
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;

if (Action != null)
Action.BeginInvoke(this, null, null);
}
}

public IEnumerable<MyItem> Children { get; set; }
public Action<MyItem> Action { get; set; }
}

这给了你以下...

imgur

...并在按顺序单击时将其吐出到控制台。

MyItem was clicked
MySubItem1 invoked action
MySubItem2 state is False



当然,在您的情况下,您可能希望将具体方法传递给委托(delegate)。

关于c# - 如何从 subview 模型绑定(bind)到父 View 模型中的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616838/

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