简体   繁体   English

WPF,选中CheckBox时更新TextBlock

[英]WPF, update TextBlock when CheckBox checked

I have a TreeView where each item has a checkbox. 我有一个TreeView ,其中每个项目都有一个复选框。 I want a TextBlock to be updated whenever an item is checked or unchecked in the TreeView . 我希望每当TreeView中的某个项目被选中或取消选中时,都将更新TextBlock The TextBlock 's Text should be bound to the CheckedVersions property on my DataContext so that when I read the CheckedVersions property, it gives me a string representing all the checked items in the TreeView . TextBlockText应被绑定到CheckedVersions我的财产DataContext这样,当我读CheckedVersions属性,它给了我代表所有在该检查的项目字符串TreeView The checked items should be represented in a semicolon-separated string. 选中的项目应以分号分隔的字符串表示。 What would be the best way to do this? 最好的方法是什么? I have the following XAML: 我有以下XAML:

<XmlDataProvider Source="XmlData/Versions.xml" XPath="//*[count(*)=0]"
                 x:Key="versionsXml"
                 IsInitialLoadEnabled="True" IsAsynchronous="False" />
<HierarchicalDataTemplate x:Key="versionTemplate">
    <CheckBox Focusable="False" IsChecked="{Binding Path=IsChecked}"
              Content="{Binding Path=Name, Mode=OneTime}"/>
</HierarchicalDataTemplate>
<TreeView x:Name="trv_version"
          ItemsSource="{Binding Path=Versions, Mode=OneWay}"
          ItemTemplate="{StaticResource versionTemplate}" />
<TextBlock x:Name="txb_version" Text="{Binding Path=CheckedVersions}"
           TextWrapping="Wrap" />

Each item in my TreeView is an instance of my VersionViewModel class, which implements INotifyPropertyChanged and notifies when the IsChecked property changes. TreeView中的每个项目都是VersionViewModel类的实例,该类实现INotifyPropertyChanged并在IsChecked属性更改时通知。 It seems like I should be able to hook into that so that when IsChecked changes on a VersionViewModel instance in the TreeView , CheckedVersions updates. 看来我应该能够了解到这一点,以便当IsCheckedTreeViewVersionViewModel实例上更改时, CheckedVersions更新。 Maybe if I set UpdateSourceTrigger on the Text binding in the TextBlock ? 也许我在TextBlockText绑定上设置UpdateSourceTrigger吗? What should I set it to, though? 但是,我应该将其设置为什么?

I think that your tree view model should "know" all the VersionViewModels and then all you need to do is register to the propertychanged event and set the "CheckedVersions" property according to the change. 我认为您的树视图模型应该“知道”所有VersionViewModel,然后您要做的就是注册到propertychanged事件并根据更改设置“ CheckedVersions”属性。

something like that: 像这样的东西:

public class treeViewModel : INotifyPropertyChanged
{
    public List<VersionViewModel> CurrentVersionViewModel { get; protected set; }

    public void AddNewVersionViewModel(VersionViewModel vvm)
    {
        CurrentVersionViewModel.Add(vvm);
        vvm.PropertyChanged += new PropertyChangedEventHandler(
            (obj,propEventArgs) => 
                {
                if (propEventArgs.PropertyName=="IsChecked")
                {
                // CheckedVersions change logic according to the new value (this is just the concept)
                    CheckedVersions += (obj as VersionViewModel).IsChecked;
                }
                }
            );
    }

    public string CheckedVersions { get { return _CheckedVersions; } set { _CheckedVersions = value; RaisePropertyChanged("CheckedVersions"); } }
    private string _CheckedVersions;

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged!=null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(prop));
        }
    }

    #endregion
}

public class VersionViewModel : INotifyPropertyChanged
{

    public bool IsChecked { get; set; }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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