如何将CustomControl属性绑定到控件本身? [英] How do I bind a CustomControl Property to the control itself?

查看:206
本文介绍了如何将CustomControl属性绑定到控件本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我问过的问题这里 -



控制类有一个依赖属性 -



private static readonly DependencyProperty
_Color = DependencyProperty.Register(Color,typeof(Color),typeof(ColorDefiner),new PropertyMetadata黑色 ) );

public Color {
get {return(Color)this.GetValue(ColorDefiner._Color); }
set {this.SetValue(ColorDefiner._Color,value); }
}



在控制XAML中,如何访问该属性,将它双向绑定到负责定义颜色属性的控件滑块?



EDIT



 < Slider 
x:Name =sdrRedHeight =32 =0Maximum =1Width =294TickPlacement =Both
TickFrequency =0.01Value ={Binding Color,Mode = TwoWay,ElementName = Me}>
< / Slider>

Me c $ c> UserControl 。我的想法是,问题是因为我指向 Color.ScR 等。



而不是绑定Slider的值到Color属性,我想我需要使用Slider Value属性将Color属性绑定到MultiBinding。



EDIT



这对我来说是一个我应该实现MVVM的地方 - 有人可以告诉我我该怎么做吗?

解决方案

如果我正确理解你想要将 ColorDefiner.Color 属性多绑定到 ColorDefiner 本身。为了在XAML中做到这一点,你需要使用一个样式(由于样式的目标类型限制,你还需要指定完全限定的属性名):

 < UserControl ...> 
< UserControl.Style>
< Style>
< Setter Property =local:ColorDefiner.Color>
< MultiBinding Converter ={StaticResource colorConverter}>
< Binding ElementName =sdrAlphaPath =ValueMode =TwoWay/>
< Binding ElementName =sdrRedPath =ValueMode =TwoWay/>
< Binding ElementName =sdrGreenPath =ValueMode =TwoWay/>
< Binding ElementName =sdrBluePath =ValueMode =TwoWay/>
< / MultiBinding>
< / Setter>
< / Style>
< UserControl.Style>
<! - sliderers - >
< / UserControl>

这种方法有一个巨大的缺点,虽然 - 如果最终用户设置控件的样式或设置自定义在颜色属性上,多重绑定将丢失。另一方面,如果最终用户为控件设置自定义内容,滑块将会消失。这就是为什么在这种情况下,你应该考虑使用 CustomControl 与模板而不是 UserControl ,并结合输出颜色代码隐藏。


This is a follow up question to the one I asked here -

The control class has a dependency property -

private static readonly DependencyProperty
    _Color = DependencyProperty.Register( "Color", typeof( Color ), typeof( ColorDefiner ), new PropertyMetadata( Colors.Black ) );

public Color Color {
    get { return ( Color )this.GetValue( ColorDefiner._Color ); }
    set { this.SetValue( ColorDefiner._Color, value ); }
}

In the control XAML, how do I access that property so that I may bind it bi-directionaly to the controls sliders responsible for defining the color property?

EDIT

This is the code for one of the sliders -

<Slider
    x:Name="sdrRed" Height="32" Minimum="0" Maximum="1" Width="294" TickPlacement="Both"
    TickFrequency="0.01" Value="{Binding Color, Mode=TwoWay, ElementName=Me}">
</Slider>

Me is the name of the UserControl. My thinking is that the problem is because I am pointing to the Color.ScR and etc.

Rather than binding the Value of the Slider to the Color property, I think I need to bind the Color property to a MultiBinding using the Slider Value properties.

EDIT

This to me feels like a place where I should implement MVVM - Can someone tell me how I might go about doing that here?

解决方案

If I understand you correctly you'd like to multi-bind the ColorDefiner.Color property to the sliders inside the ColorDefiner itself. In order to do it in XAML, you'll need to do it using a style (you'll also need to specify fully qualified property name due to the style's target type restriction):

<UserControl ...>
    <UserControl.Style>
        <Style>
            <Setter Property="local:ColorDefiner.Color">
                <MultiBinding Converter="{StaticResource colorConverter}">
                    <Binding ElementName="sdrAlpha" Path="Value" Mode="TwoWay" />
                    <Binding ElementName="sdrRed" Path="Value" Mode="TwoWay" />
                    <Binding ElementName="sdrGreen" Path="Value" Mode="TwoWay" />
                    <Binding ElementName="sdrBlue" Path="Value" Mode="TwoWay" />
                </MultiBinding>
            </Setter>
        </Style>
    <UserControl.Style>
    <!-- sliders -->
</UserControl>

This approach has a huge drawback though - if end user sets a style for the control or sets a custom biding on the Color property, the multi-binding will be lost. On the other hand, if end user sets custom content for the control the sliders will be gone. That's why in this scenario you should really consider using CustomControl together with a template instead of UserControl and combine the output color in code-behind.

这篇关于如何将CustomControl属性绑定到控件本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆