English 中文(简体)
如何在习俗控制中自动形成约束?
原标题:How to create binding inside custom control automatically?

我有我的习惯工具,对依赖性财产进行控制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.

现在我还有4个这样的财产。 我不想在XAML中添加这些内容,而是想知道如何对这一条加以约束?

EDIT

实际上,我对我的回答是:。 银根:具有方案约束性的控制特性

现在,我的问题是:

在类似情况下适用这种约束力是否正确?

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

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

我是否应当检查一下,XAML是否对这一财产有约束力(另一个约束性)? 这种做法是行不通的,但我不知道它是否对业绩、影响等造成坏错?

http://code>onApplyTemplate。 这是否更好?

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

如果你试图利用这种控制来看待没有IsBusy财产的模式,那将是坏的,但即便如此,你也只收到产出窗口的警告,而没有任何担心。

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.

这样,如果您的受抚养经验确实有某种财产被改换回来,并且只用于诸如<代码>-Template IsBusy}等控制模板,则你可以用<代码>{具有约束力的IsBusy}取代这一条。 与此类似,要么采用具有约束力的或数据触发器:

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

这一概念很简单:TemplateBled适用于控制的依赖特性,而Bled则适用于数据标的或观点模型的特性,并且可以在没有问题的情况下共存。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签