如何绑定到此自定义依赖项属性?

时间:2022-06-19 06:32:48

I have a DependencyProperty in my custom UserControl that looks like this:

我的自定义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); }
}

In Expression Blend 3, I have this:

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

Now, what I'd like to do is make the ColumnWidth dependency property control the width of the TextBlock in the EmployeeHeaderTemplate DataTemplate and the ListBox in the CallListTemplate DataTemplate. I know I can do this in C#, but I have a feeling it'd also be possible with pure XAML data-binding.

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

But, being relatively new to Silverlight and Expression Blend 3, I'm not sure how to go about this. Any suggestions?

但是,对于Silverlight和Expression Blend 3来说相对较新,我不知道如何解决这个问题。有什么建议么?

2 个解决方案

#1


1  

Try put a name on your CallBoard instance and then refer to that using ElementName in your Binding.

尝试在CallBoard实例上添加一个名称,然后在Binding中使用ElementName引用它。

So the root of your page would look like:

所以你的页面的根看起来像:

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

and your Binding would look like:

你的Binding看起来像:

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

#2


0  

Does Width="{Binding ColumnWidth}" not work?

Width =“{Binding ColumnWidth}”不起作用吗?

#1


1  

Try put a name on your CallBoard instance and then refer to that using ElementName in your Binding.

尝试在CallBoard实例上添加一个名称,然后在Binding中使用ElementName引用它。

So the root of your page would look like:

所以你的页面的根看起来像:

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

and your Binding would look like:

你的Binding看起来像:

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

#2


0  

Does Width="{Binding ColumnWidth}" not work?

Width =“{Binding ColumnWidth}”不起作用吗?