[英]WPF UserControl with nullable dependency property dependent on checkbox value
I'm trying to create a custom UserControl that displays the properties of a complex object as a form.我正在尝试创建一个自定义 UserControl,它将复杂对象的属性显示为表单。 Additionally, if the user unchecks a checkbox in the header of the UserControl, the value of the dependency property should be null (but the form values should stay displayed, even if the form is disabled) - and vice versa.
此外,如果用户取消选中 UserControl 标题中的复选框,则依赖项属性的值应为 null(但表单值应保持显示,即使表单被禁用) - 反之亦然。
I'm working with two dependency properties of type ComplexObject on the UserControl - one is public (this will be bound to by the client), another is private (its properties will be bound to the internal controls in the UserControl):我正在 UserControl 上使用两个 ComplexObject 类型的依赖属性 - 一个是公共的(这将由客户端绑定),另一个是私有的(其属性将绑定到 UserControl 中的内部控件):
public ComplexObject ComplexObject
{
get { return (ComplexObject )GetValue(ComplexObjectProperty); }
set { SetValue(ComplexObjectProperty, value); }
}
private ComplexObject VisibleComplexObject
{
get { return (ComplexObject)GetValue(VisibleComplexObjectProperty); }
set { SetValue(VisibleComplexObjectProperty, value); }
}
Now I'm struggling with a binding between those two, so that CompexObject becomes either VisibleComplexObject or null based on the checkbox value.现在我正在努力解决这两者之间的绑定问题,以便 CompexObject 根据复选框值变为 VisibleComplexObject 或 null。 This should also work the other way.
这也应该以另一种方式工作。 I've tried to solve this using DataTriggers in the Style of the UserControl, but was unable to do so:
我尝试使用 UserControl 样式中的 DataTriggers 解决此问题,但无法这样做:
<UserControl.Style>
<Style TargetType="local:CheckableComplexTypeGroup">
// 'CheckableComplexTypeGroup' TargetType does not match type of the element 'UserControl'
</Style>
</UserControl.Style>
Using <local:CheckableComplexTypeGroup.Style>
instead of <UserControl.Style>
didn't work either.使用
<local:CheckableComplexTypeGroup.Style>
而不是<UserControl.Style>
也不起作用。
Are there any other suggestions?还有其他建议吗? Or maybe another way of doing this?
或者也许是另一种方式来做到这一点?
Finally I've solved this without using plain old event handlers instead of binding/triggers.最后,我在不使用普通旧事件处理程序而不是绑定/触发器的情况下解决了这个问题。
CheckableComplexObjectGroup.xaml: CheckableComplexObjectGroup.xaml:
<UserControl Name="thisUC" ...>
<GroupBox>
<GroupBox.Header>
<CheckBox Name="cbComplexObject"
IsChecked="{Binding ElementName=thisUC, Path=ComplexObject, Mode=OneWay, Converter={StaticResource nullToFalseConv}}"
Checked="cbComplexObject_Checked" Unchecked="cbComplexObject_Unchecked"/>
</GroupBox.Header>
...
</UserControl>
CheckableComplexObjectGroup.cs: CheckableComplexObjectGroup.cs:
public static readonly DependencyProperty ComplexObjectProperty =
DependencyProperty.Register("ComplexObject",
typeof(ComplexObject),
typeof(CheckableComplexObjectGroup),
new PropertyMetadata(null));
private static readonly DependencyProperty VisibleComplexObjectProperty =
DependencyProperty.Register("VisibleComplexObject",
typeof(ComplexObject),
typeof(CheckableComplexObjectGroup),
new PropertyMetadata(new ComplexObject()));
//...
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
if (e.Property == ComplexObjectProperty)
{
if (null != e.NewValue)
VisibleComplexObject = ComplexObject;
}
base.OnPropertyChanged(e);
}
private void cbComplexObject_Checked(object sender, RoutedEventArgs e)
{
ComplexObject = VisibleComplexObject;
}
private void cbComplexObject_Unchecked(object sender, RoutedEventArgs e)
{
ComplexObject = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.