我有DataDependentControl,其中定义了依赖项属性: TextVisibility,CalendarVisibility,ComboControlVisibility 。 当我使用枚举数据类型在外部组合框中选择一些值时,上面提到的属性会更新为 可见性 枚举值,但绑定不会更新内部控件上的Visibility属性:< / p>
<Views:DataDependentControl x:Name="typeValue">
<StackPanel x:Name="container">
<TextBox x:Name="TextBoxControl" Visibility="{Binding ElementName=typeValue, Path=TextVisibility, Mode=OneWay}"/>
<Controls:Calendar x:Name="CalendarControl" HorizontalAlignment="Left" Visibility="{Binding ElementName=typeValue, Path=CalendarVisibility}"/>
<ComboBox x:Name="ComboBoxControl" Visibility="{Binding ElementName=typeValue, Path=ComboControlVisibility}"/>
</StackPanel>
</Views:DataDependentControl>
DataDependentControl具有 DataType 属性。 根据DataType,我通过以下属性隐藏或折叠子控件: ComboControlVisibility,CalendarVisibility或TextVisibility
DataDependentControl中的DataType 属性定义如下:
public static readonly DependencyProperty DataTypeProperty = DependencyProperty.Register(
DataTypePropertyName,
typeof (DataTypeEnum),
typeof (DataDependentControl),
new PropertyMetadata(ValueChangedCallback));
ValueChangedCallback 函数定义如下:
private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var thisObject = (DataDependentControl)d;
var newValue = (DataTypeEnum) e.NewValue;
switch (newValue)
{
case DataTypeEnum.Bool:
thisObject.ComboControlVisibility = Visibility.Visible;
thisObject.CalendarVisibility = Visibility.Collapsed;
thisObject.TextVisibility = Visibility.Collapsed;
...
TextVisibility,CalendarVisibility或ComboControlVisibility的示例位于:
public static readonly DependencyProperty TextVisibilityProperty = DependencyProperty.Register(
TextVisibilityPropertyName,
typeof (Visibility),
typeof (DataDependentControl),
null);
总而言之:
在<StackPanel/>
:
如果选择的数据类型是“string”,则应该是可见的
<TextBox x:Name="TextBoxControl"/>
如果选择的数据类型为“DateTime”,则应显示
<Controls:Calendar/>
如果选择的数据类型为“布尔”,则应该是可见的
<ComboBox/>
DataDependentControl具有“DataType”属性,该属性在组合框中定义。 “DataType”是数据绑定:
<Views:DataDependentControl x:Name="typeValue"
DataType="{Binding SelectedItem, ElementName=DataTypes, Converter={StaticResource DataTypeReverseConverter}}"
答案 0 :(得分:0)
在我看来,您正在ComboBox
尝试依赖属性,即使它们是由DataDependentControl
定义的。如果子控件的可见性与ComboBox
的可见性相关联,为什么不这样做呢?
<Views:DataDependentControl x:Name="typeValue">
<StackPanel x:Name="container">
<TextBox x:Name="TextBoxControl" Visibility="{Binding Visibility, ElementName=typeValue}"/>
<Controls:Calendar x:Name="CalendarControl" HorizontalAlignment="Left" Visibility="{Binding Visibility, ElementName=typeValue}"/>
<ComboBox x:Name="ComboBoxControl" Visibility="{Binding Visibility, ElementName=typeValue}"/>
</StackPanel>
</Views:DataDependentControl>
或者,甚至更好:
<Views:DataDependentControl x:Name="typeValue">
<StackPanel x:Name="container" Visibility="{Binding Visibility, ElementName=typeValue}">
<TextBox x:Name="TextBoxControl"/>
<Controls:Calendar x:Name="CalendarControl" HorizontalAlignment="Left"/>
<ComboBox x:Name="ComboBoxControl"/>
</StackPanel>
</Views:DataDependentControl>