开发者

WPF, update TextBlock when CheckBox checked

I have a TreeView where each item has a checkbox. I want a TextBlock to be updated whenever an item is checked or unchecked in the TreeView. 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. 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:

<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. 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. Maybe if I set UpdateSourceTrigger on the Text binding in the TextBlock? 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.

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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜