绑定不会更新属性

时间:2010-08-31 08:43:56

标签: silverlight data-binding

我有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}}"

1 个答案:

答案 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>