[英]Custom WPF binding not being updated on listbox selection
I'm puzzled by this probably trivial matter. 我对这个可能无关紧要的事情感到困惑。 I have my custom property on a descendant of
DevExpresses
class LayoutGroup
(shouldn't make any difference IMO): 我在
DevExpresses
类LayoutGroup
的后代中具有自定义属性(IMO不应有任何区别):
public class ExpandableLayoutGroup : LayoutGroup
{
public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(Boolean), typeof(ExpandableLayoutGroup));
public Boolean IsExpanded
{
get { return (Boolean) GetValue(IsExpandedProperty); }
set
{
expandCheckBox.IsChecked = value;
SetValue(IsExpandedProperty, value);
}
}
}
Then I have XAML binding to a listbox (containing 2 items at this time) called listStates
. 然后,我将XAML绑定到名为
listStates
的列表框(此时包含2个项目)。
<ExpandableLayoutGroup x:Name="groupTool" IsExpanded="{Binding SelectedValue.IsTool, ElementName=listStates}" DataContext="{Binding Tool}" Header="Tool" View="GroupBox" />
The object list binding to listStates contains both properties (simplified): 绑定到listStates的对象列表包含两个属性(简化):
public class State
{
public Boolean IsItemTool { get; private set; }
public Tool Tool { get; private set; }
}
Am I missing something obvious? 我是否缺少明显的东西? Because I would expect that when I change listbox selection (single) it would also update
IsExpanded
property on a group. 因为我希望当我更改列表框选择(单个)时,它也会更新组上的
IsExpanded
属性。 I have more subproperties of Tool binded (as apparent by DataContext="{Binding Tool}"
) and those update well. 我具有绑定的工具的更多子属性(如
DataContext="{Binding Tool}"
),这些属性更新得很好。 So the DataContext
is changing correctly on listbox selection. 因此,
DataContext
在列表框选择上正确更改。 Except this one property IsExpanded
(that should expand/collapse layoutgroup). 除了这一属性,
IsExpanded
(应展开/折叠布局组)。
What am I doing wrong, and how to make it so, that when listbox changes, IsExpanded
binding is polled to get value from IsTool
, and will set it (depending on selection). 我做错了什么,以及如何做到这一点,所以当列表框更改时,
IsExpanded
绑定进行轮询以从IsTool
获取值,并将其进行设置(取决于选择)。
Getters and setters of dependency properties must contain only GetValue
and SetValue
calls, because there're two ways to get and set their values: using getters and setters and using DependencyProperty
or DependencyPropertyKey
. 依赖项属性的getter和setter必须仅包含
GetValue
和SetValue
调用,因为有两种获取和设置其值的方法:使用getter和setter以及使用DependencyProperty
或DependencyPropertyKey
。 This is why code in your setter is not executed (bindings don't use them for dependency properties). 这就是为什么不执行setter中的代码的原因(绑定不会将它们用于依赖项属性)。 If you want some code to be executed when the value is changed, specify it as a
PropertyChangedCallback
callback in PropertyMetadata
. 如果希望在更改值时执行某些代码,请在
PropertyMetadata
中将其指定为PropertyChangedCallback
回调。
See PropertyChangedCallback Delegate on MSDN . 请参见MSDN上的PropertyChangedCallback委托 。
The setter for a dependency property must only set the value for it's underlying type. 依赖项属性的设置器只能为其基础类型设置值。 The .NET internals reserve the right to call
SetValue()
directly (and in my experience WPF usually does, while Silverlight uses the setter that you've defined). .NET内部人员保留直接调用
SetValue()
的权利(根据我的经验,WPF通常会这样做,而Silverlight使用您已定义的setter)。
Refer to the "Implications for Custom Dependency Properties" section of XAML Loading and Dependency Properties for details 有关详细信息,请参阅XAML加载和依赖项属性的“自定义依赖项属性的含义”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.