如何绑定到此自定义依赖属性? [英] How do I bind to this custom dependency property?

查看:142
本文介绍了如何绑定到此自定义依赖属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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中,我有:

In Expression Blend 3, I have this:

<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依赖属性控制TextBlock的宽度在EmployeeHeaderTemplate DataTemplate和ListBox中的CallListTemplate DataTemplate。我知道我可以在C#中做到这一点,但是我也有可能用纯XAML数据绑定。

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.

但是,相对来说,Silverlight和表达式混合3,我不知道该怎么做。任何建议?

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

推荐答案

尝试在您的CallBoard实例上放一个名字,然后参考在Binding中使用ElementName。

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

所以页面的根目录将如下所示:

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

,您的Binding将如下所示:

and your Binding would look like:

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

这篇关于如何绑定到此自定义依赖属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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