【问题标题】:How do I bind to this custom dependency property?如何绑定到这个自定义依赖属性?
【发布时间】:2010-11-25 22:59:50
【问题描述】:

我的自定义 UserControl 中有一个 DependencyProperty,如下所示:

public static readonly DependencyProperty ColumnWidthProperty =
   DependencyProperty.Register("ColumnWidth", typeof(int), typeof(CallBoard),
       new PropertyMetadata(150));

public int ColumnWidth {
    get { return (int)GetValue(ColumnWidthProperty); }
    set { SetValue(ColumnWidthProperty, value); }
}

在 Expression Blend 3 中,我有这个:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="SilverlightTest.CallBoard"
    d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <DataTemplate x:Key="EmployeeHeaderTemplate">
            <TextBlock Text="{Binding Name}" TextAlignment="Center" FontWeight="Bold" FontSize="16"/>
        </DataTemplate>
        <DataTemplate x:Key="CallListItemTemplate">
            <StackPanel >
                <TextBlock Text="{Binding CustomerName}" FontWeight="Bold"/>
                <TextBlock Text="{Binding Details}"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="CallListTemplate">
            <ListBox ItemTemplate="{StaticResource CallListItemTemplate}" ItemsSource="{Binding Calls}"/>
        </DataTemplate>
    </UserControl.Resources>
    <StackPanel x:Name="stackPanel" DataContext="{Binding Source={StaticResource DummyDataSource}}">
        <ItemsControl ItemsPanel="{StaticResource HorizontalItemsPanelTemplate}" ItemTemplate="{StaticResource EmployeeHeaderTemplate}" ItemsSource="{Binding}"/>
        <ItemsControl ItemsPanel="{StaticResource HorizontalItemsPanelTemplate}" ItemTemplate="{StaticResource CallListTemplate}" ItemsSource="{Binding}"/>
    </StackPanel>
</UserControl>

现在,我想做的是让 ColumnWidth 依赖属性控制 EmployeeHeaderTemplate DataTemplate 中的 TextBlock 和 CallListTemplate DataTemplate 中的 ListBox 的宽度。我知道我可以在 C# 中做到这一点,但我感觉它也可以通过纯 XAML 数据绑定来实现。

但是,作为 Silverlight 和 Expression Blend 3 的新手,我不知道该怎么做。有什么建议吗?

【问题讨论】:

    标签: silverlight data-binding datatemplate expression-blend dependency-properties


    【解决方案1】:

    尝试在 CallBoard 实例上命名,然后在 Binding 中使用 ElementName 引用该名称。

    所以你的页面的根应该是这样的:

        <UserControl
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Class="SilverlightTest.CallBoard"
            x:Name="callBoard"
            ...
        >
        ...
    

    你的绑定看起来像:

    Width="{Binding ElementName=callBoard, Path=ColumnWidth}"
    

    【讨论】:

    • 该方法的问题是我将无法在同一页面上使用控件的多个实例。我刚刚决定在代码隐藏文件中进行绑定。
    【解决方案2】:

    Width="{Binding ColumnWidth}" 不工作吗?

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2012-08-29
      • 2013-06-20
      • 2011-12-20
      • 2012-12-07
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      相关资源
      最近更新 更多