如何在wpf中绑定treeview列的属性值

时间:2017-01-12 14:57:59

标签: c# wpf data-binding treeview

这是我的一部分XAML:

    <Grid>
                <dxg:TreeListControl x:Name="HierarchyTreeControl" ItemsSource="{Binding MenuItems}" EnableSmartColumnsGeneration="False" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" SelectionMode="Row" AutoGenerateColumns="None">
                    <dx:TreeListColumn FieldName="Name" Header="Title" Width="50" />
                    <dx:TreeListColumn FieldName="URL" Header="Link" Width="150" />             
                    <dx:TreeListColumn Binding="{Binding HasChildren}" Header="Has any children" Width="20" BestFitArea="Header" AllowResizing="False" ShowInColumnChooser="True" Visible="True" >
                        <dx:TreeListColumn.EditSettings>
                            <dxe:CheckEditSettings />
                        </dx:TreeListColumn.EditSettings>
                    </dx:TreeListColumn>
                    <dx:TreeListColumn Binding="{Binding IsChecked}" Header="Удалить" ReadOnly="False" AllowEditing="{Binding Path=HasChildren, Converter={StaticResource BoolToFalseConverter}, UpdateSourceTrigger=PropertyChanged}" Width="20" BestFitArea="Header" AllowResizing="False" ShowInColumnChooser="True" Visible="True" >
                        <dx:TreeListColumn.EditSettings>
                            <dxe:CheckEditSettings />
                        </dx:TreeListColumn.EditSettings>
                    </dx:TreeListColumn>
                    <dxg:TreeListControl.View>
                        <dxg:TreeListView AllowPerPixelScrolling="True" ShowTotalSummary="False" KeyFieldName="ID" ParentFieldName="ParentID"  TreeDerivationMode="Selfreference" AllowEditing="False" AutoExpandAllNodes="False" FetchSublevelChildrenOnExpand="False" AllowColumnMoving="False" AutoWidth="True" ShowNodeImages="False" />
                    </dxg:TreeListControl.View>
                </dxg:TreeListControl>
            </Grid>

源{MenuItems}为ObservableCollection<MenuItem>

我想仅在属性MenuItem.HasChildren = false时检查最后一列中的复选框

我正在尝试使用此代码:

  

AllowEditing =&#34; {Binding Path = HasChildren,Converter = {StaticResource   BoolToFalseConverter},UpdateSourceTrigger = PropertyChanged}&#34;

但这不起作用,我看到转换器BoolToFalseConverter只调用一次,第一行

如何完成任务?

更新: 这是MenuItem的类

public class MenuItem : INotifyPropertyChanged
    {
        private int _id;
        public int ID
        {
            get {return _id;}
            set
            {
                if (_id == value)
                    return;
                _id = value;
                OnPropertyChanged("ID");
            }
        }

        private string _name;
        public string Name
        {
            get{return _name;}
            set
            {
                if (_name == value)
                    return;
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private string _url;
        public string URL
        {
            get
            {
                return _url;
            }
            set
            {
                if (_url == value)
                    return;
                _url = value;
                OnPropertyChanged("URL");
            }
        }

        private int _target;
        public int Target
        {
            get{return _target;}
            set
            {
                if (_target == value)
                    return;
                _target = value;
                OnPropertyChanged("Target");
            }
        }
        private int? _parentID;
        public int? ParentID
        {
            get{return _parentID;}
            set
            {
                if ((_parentID == null && value == null) || _parentID == value)
                    return;
                _parentID = value;
                OnPropertyChanged("ParentID");
            }
        }

        public bool HasChildren { get; set; }

        private bool _isChecked;
        public bool IsChecked
        {
            get{return _isChecked;}
            set
            {
                if (_isChecked == value)
                    return;
                _isChecked = value;
                OnPropertyChanged("IsChecked");
            }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

1 个答案:

答案 0 :(得分:1)

我假设HasChildren引用了“查看”孩子。在这种情况下,您需要一个将发布事件的专用转换器(或更好的行为)。视图模型将订阅该事件,一旦收到它将设置为true或false HasChildren的值,并通知Is Checked属性以通知视图。

如果HasChildren与viewmodel对象相关,那么它只是一种在视图模型中进行boll检查的方式并通知视图。

从评论中更新:

我在DevExpress论坛中发现了这个问题,该问题正在谈论你的问题=&gt; devexpress.com/Support/Center/Question/Details/Q367068他们在那里说你应该使用TreeListView.ShowingEditor事件,在调用时你可以设置启用或禁用特定单元格的编辑