相关疑难解决方法(0)

自定义控件依赖项属性绑定

即使是最基本的例子,我也会疯狂地试图让它工作.我不能为我的生活得到约束力.这是一个不适合我的超级简单示例.我必须做错事.

我的控件库程序集中的自定义控件:

public class TestControl : Control
{
    public static readonly DependencyProperty TestPropProperty =
        DependencyProperty.Register("TestProp", typeof(string), typeof(TestControl), new UIPropertyMetadata(null));

    public string TestProp
    {
        get { return (string)GetValue(TestPropProperty); }
        set { SetValue(TestPropProperty, value); }
    }

    static TestControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl)));
    }
}
Run Code Online (Sandbox Code Playgroud)

它的XAML模板:

<Style TargetType="{x:Type local:TestControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TestControl}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel>
                        <TextBlock Text="Testing..." />
                        <Label Content="{Binding TestProp}" Padding="10" />
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

这是XAML在wpf窗口中使用控件来引用我的控件库:

<Grid>
    <ItemsControl Name="mylist">
        <ItemsControl.ItemTemplate>
            <DataTemplate> …
Run Code Online (Sandbox Code Playgroud)

wpf dependency-properties wpf-controls

22
推荐指数
1
解决办法
2万
查看次数

标签 统计

dependency-properties ×1

wpf ×1

wpf-controls ×1