如何创建自定义自动控件内绑定? [英] How to create binding inside custom control automatically?

查看:101
本文介绍了如何创建自定义自动控件内绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的DependencyProperty自定义工具栏控制 IsBusy

下面是我如何使用它:

 <控制:myToolbar
                Grid.ColumnSpan =5模式=DataEntry
                状态={绑定状态,转换器= {StaticResource的ViewEditingStateToToolbarStateConverter}}
                IsBusy ={结合IsBusy}/>

按照惯例从基本VM的所有我的虚拟机的继承和拥有财产IsBusy。
所以,我知道,这家酒店将始终可用的虚拟机。

现在我有另外4个特性是这样的。相反,将其添加到XAML我所有的意见,我想知道如何绑定到该 IsBusy 内部控制的code自动所以我不必在XAML绑定?

修改

其实,我发现回答我的问题:<一href=\"http://stackoverflow.com/questions/1126490/silverlight-programmatically-binding-control-properties\">Silverlight:结合编程控件属性

现在,我的问题是:

是不是正确,这样的构造应用此绑定?

 公共myToolbar()
        {
            this.DefaultStyleKey = typeof运算(myToolbar);            VAR约束力=新的绑定(IsBusy){模式= BindingMode.TwoWay};
            this.SetBinding(IsBusyProperty,绑定);
        }

我应该检查是否XAML绑定(另一个绑定)存在这个属性,而不是绑定?它的工作原理无论哪种方式,但我不知道这是不好的表现,气味等?

怎么样在 onApplyTemplate 这样做。那是更好的办法?

 如果(GetBindingEx pression(IsBusyProperty)== NULL)
            {
                VAR约束力=新的绑定(IsBusy){模式= BindingMode.TwoWay};
                this.SetBinding(IsBusyProperty,绑定);
            }


解决方案

这将如果你试图用这种控制与不具备 IsBusy 属性,但即使如此,你只会收到在输出窗口调试警告,没什么可担心的。

至于绑定的地方,如果你绑定到依赖属性不执行其回调内部的任何动作的构造是适当的。
但如果该属性更改回调试图调用等功能 GetTemplateChild 和检索内部控制 - 那么你应该将绑定到 OnApplyTemplate 的功能,因为只有在那里,你可以放心,内部控制存在的。

顺便说一句,如果你的依赖媒体资源相关联没有一个属性更改回调,并且只在如 {TemplateBinding IsBusy} ,可以代替这个控件模板使用通过行{结合IsBusy} 。像这样,通过使用绑定或数据触发:

 &LT;的ControlTemplate的TargetType ={X:类型控件:myToolbar}&GT;
    &LT;网格和GT;
        &LT; ContentControl中X:名称=内容... /&GT;
        &LT;进度X:NAME =进步... /&GT;
    &LT; /网格和GT;
    &LT; ControlTemplate.Triggers&GT;
        &LT; D​​ataTrigger绑定={结合IsBusy}VALUE =真&GT;
            &LT;二传手的TargetName =进步属性=能见度VALUE =可见/&GT;
        &LT; / DataTrigger&GT;

这个想法很简单: TemplateBinding 应用于控制的依赖属性,而绑定适用于性能DataContext对象或视图模型和他们可以没有问题并存。

I have my custom toolbar control with DependencyProperty IsBusy

Here is how I use it:

<Controls:myToolbar 
                Grid.ColumnSpan="5" Mode="DataEntry" 
                Status="{Binding State, Converter={StaticResource ViewEditingStateToToolbarStateConverter}}"
                IsBusy="{Binding IsBusy}"/>

By convention all my VM's inherit from base VM and have IsBusy property. So, I KNOW that this property will always be available on VM.

Now I have another 4 properties like this. Instead of adding them to XAML on all my views I want to know how to bind to this IsBusy automatically inside control's code so I don't have to bind in XAML?

EDIT

Actually, I found answer to my question: Silverlight: Programmatically binding control properties

Now, my question is:

Is it correct to apply this binding in constructor like this?

public myToolbar()
        {
            this.DefaultStyleKey = typeof(myToolbar);

            var binding = new Binding("IsBusy") { Mode = BindingMode.TwoWay };
            this.SetBinding(IsBusyProperty, binding); 
        }

Should I check if XAML binding (another binding) exist to this property and not bind? It works either way but I wonder if it's bad for performance, smells, etc?

What about doing this in onApplyTemplate. Is that better way?

if (GetBindingExpression(IsBusyProperty) == null)
            {
                var binding = new Binding("IsBusy") { Mode = BindingMode.TwoWay };
                this.SetBinding(IsBusyProperty, binding);
            }

解决方案

It would be bad if you tried to use this control with a view model which doesn't have the IsBusy property, but even then you'll receive just a debug warning in the output window, nothing to worry about.

As to the place of the binding, the constructor is appropriate if the dependency property which you are binding to doesn't perform any actions inside its callback. But if the property changed callback tries to call such functions as GetTemplateChild and retrieve inner controls - then you should move the binding to the OnApplyTemplate functions, because only there you can be assured that inner controls exist.

By the way, if your dependency proeprty doesn't have a property changed callback and is used only in the control template like {TemplateBinding IsBusy}, you can replace this line by {Binding IsBusy}. Something like this, either by using binding or data triggers:

<ControlTemplate TargetType="{x:Type Controls:myToolbar}">
    <Grid>
        <ContentControl x:Name="content" ... />
        <ProgressBar x:name="progress" ... />
    </Grid>
    <ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding IsBusy}" Value="True">
            <Setter TargetName="progress" Property="Visibility" Value="Visible" />
        </DataTrigger>

The idea is simple: TemplateBinding is applied to dependency properties of the control, whereas Binding is applied to properties of the DataContext object or the view model and they can coexist without problems.

这篇关于如何创建自定义自动控件内绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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