开发者

Binding doesn't update the property

I have DataDependentControl with dependency properties defined: TextVisibility, CalendarVisibility, ComboControlVisibility. When I select some values in outside combobox with enumerated datatypes, the properties mentioned above are updated to Visibility enumeration value, but binding doesn't update Visibility property on inside controls:

        <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 has DataType property. According to DataType I hide or collapse child controls via following properties: ComboControlVisibility, CalendarVisibility or TextVisibility

DataType property within DataDependentControl is defined as follows:

public static readonly DependencyProperty DataTypeProperty = DependencyProperty.Register(
            DataTypePropertyName,
            typeof (DataTypeEnum),
            typeof (DataDependentControl),
            new PropertyMetadata(ValueChangedCallback));

ValueChangedCallback function is defined as follows:

private static void ValueChangedCallb开发者_运维技巧ack(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;

....

Example of TextVisibility, CalendarVisibility or ComboControlVisibility is here:

public static readonly DependencyProperty TextVisibilityProperty = DependencyProperty.Register(
            TextVisibilityPropertyName,
            typeof (Visibility),
            typeof (DataDependentControl),
            null);

All in all:

Inside the <StackPanel/>:

If selected datatype is "string" there should be visible

<TextBox x:Name="TextBoxControl"/>

If selected datatype is "DateTime" there should be visible

<Controls:Calendar/>

If selected datatype is "Boolean" there should be visible

<ComboBox/>

DataDependentControl has "DataType" property which is defined in combobox. "DataType" is databound:

<Views:DataDependentControl x:Name="typeValue"
                                        DataType="{Binding SelectedItem, ElementName=DataTypes, Converter={StaticResource DataTypeReverseConverter}}"


Looks to me like you're trying to your dependency properties on a ComboBox, even though they're defined by your DataDependentControl. If the visibility of the child controls is tied to the visibility of the ComboBox, why not just do this?

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

Or, even better:

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜