绑定到CustomControls问题

时间:2012-01-01 21:02:56

标签: wpf xaml data-binding mvvm custom-controls

我有一个自定义控件,它使用名为State的dependcy属性从TextBox继承。 状态为枚举,有4个选项{Mismatch,Wait,Ok,None}

Generic看起来像这样:

<Style TargetType="{x:Type local:CustomTextBox}">
    <Style.Triggers>
        <Trigger Property="State" Value="Mismatch">
            <Setter Property="Background" Value="{StaticResource MismatchBrush}"/>
        </Trigger>
    </Style.Triggers>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomTextBox}">
                <Border Background="{TemplateBinding Background}"
                        BorederBrush="{TemplateBinding BorederBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

我有类名为MyData的类,其属性为DataState,Sent,Recieved每个属性实现INotifyPropertyChanged。

在我的主窗口中,我在xaml中有以下行:

<custom:CustomTextBox State="{Binding BindingData.DataState}" Text="{Binding BindedData.Sent}"/>

在我的视图模型中,我有MyData类型的字段,调用BindedData,当用户发送一些数据时,BindedData.Sent会发生变化,然后如果出现某些逻辑,BindedData状态会变为Mismatch,而customTextBox背景需要是某种颜色

问题在于背景不会改变其意味着不工作的状态的绑定以及文本绑定不起作用。

我知道绑定是如何工作的,以及它在其他情况下的工作(不是自定义控件),在这里我也检查了调试,我看到一切正常,所以我犯了customcontrol的错误吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

也许您从主窗口再次设置背景? 我试过复制你的代码并且它完美地工作,除非我从主窗口设置背景(因为它覆盖了样式设置器)