如何绑定到这个自定义依赖属性?

发布于 2024-08-04 04:06:24 字数 2335 浏览 2 评论 0原文

我的自定义 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 和 ListBox 中 TextBlock 的宽度在 CallListTemplate 数据模板中。我知道我可以在 C# 中做到这一点,但我有一种感觉,使用纯 XAML 数据绑定也可以做到这一点。

但是,由于对 Silverlight 和 Expression Blend 3 相对较新,我不确定如何解决这个问题。有什么建议吗?

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); }
}

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>

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄情伤 2024-08-11 04:06:24

尝试在 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}"

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

and your Binding would look like:

Width="{Binding ElementName=callBoard, Path=ColumnWidth}"
你的他你的她 2024-08-11 04:06:24

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文