具有可空依赖项 属性 的 WPF UserControl 取决于复选框值
WPF UserControl with nullable dependency property dependent on checkbox value
我正在尝试创建一个自定义 UserControl,它以表单形式显示复杂 object 的属性。此外,如果用户取消选中 UserControl 的 header 中的复选框,则依赖项 属性 的值应该为空(但表单值应该保持显示,即使表单被禁用) - 和反之亦然。
我在 UserControl 上使用 ComplexObject 类型的两个依赖属性 - 一个是 public(这将由客户端绑定),另一个是私有的(它的属性将绑定到内部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); }
}
现在我正在努力解决这两者之间的绑定问题,以便 CompexObject 根据复选框值变为 VisibleComplexObject 或 null。这也应该以其他方式工作。我尝试在 UserControl 的样式中使用 DataTriggers 来解决这个问题,但无法这样做:
<UserControl.Style>
<Style TargetType="local:CheckableComplexTypeGroup">
// 'CheckableComplexTypeGroup' TargetType does not match type of the element 'UserControl'
</Style>
</UserControl.Style>
使用 <local:CheckableComplexTypeGroup.Style>
而不是 <UserControl.Style>
也不起作用。
还有其他建议吗?或者也许是另一种方法?
最后我解决了这个问题而不是使用普通的旧事件处理程序而不是 binding/triggers。
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:
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;
}
我正在尝试创建一个自定义 UserControl,它以表单形式显示复杂 object 的属性。此外,如果用户取消选中 UserControl 的 header 中的复选框,则依赖项 属性 的值应该为空(但表单值应该保持显示,即使表单被禁用) - 和反之亦然。
我在 UserControl 上使用 ComplexObject 类型的两个依赖属性 - 一个是 public(这将由客户端绑定),另一个是私有的(它的属性将绑定到内部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); }
}
现在我正在努力解决这两者之间的绑定问题,以便 CompexObject 根据复选框值变为 VisibleComplexObject 或 null。这也应该以其他方式工作。我尝试在 UserControl 的样式中使用 DataTriggers 来解决这个问题,但无法这样做:
<UserControl.Style>
<Style TargetType="local:CheckableComplexTypeGroup">
// 'CheckableComplexTypeGroup' TargetType does not match type of the element 'UserControl'
</Style>
</UserControl.Style>
使用 <local:CheckableComplexTypeGroup.Style>
而不是 <UserControl.Style>
也不起作用。
还有其他建议吗?或者也许是另一种方法?
最后我解决了这个问题而不是使用普通的旧事件处理程序而不是 binding/triggers。
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:
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;
}