使用自定义面板问题将附加属性绑定到ItemsControl中的项目 [英] Binding an attached property to an item in ItemsControl with custom panel problem

查看:69
本文介绍了使用自定义面板问题将附加属性绑定到ItemsControl中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法按照需要运行以下XAML.所有绑定均有效,因为绑定不会导致任何错误.但是我没有从 RatioShape 矩形上进行绑定得到预期的结果.问题在于附加属性 wpflib:RatioPanel.Ratio 始终返回其默认值,而不是数据绑定值.

I can't get the following XAML to work as I want. All bindings work because get no errors from the bindings. But I don't get the expected result from the binding on the RatioShape rectangle. The problem is that the attached property wpflib:RatioPanel.Ratio always returns its default value, not the databound value.

因此,我认为 RatioShape 上的附加属性设置为错误的上下文".如何绑定到附加属性,以便 wpflib:RatioPanel 获得绑定值?

So I'm thinking that the attached property on RatioShape is set in the wrong "context". How do bind to the attached property so that wpflib:RatioPanel gets the bound value?

<wpflib:RatioContentPresenter2 RatioMaxValue="{Binding Path=RatioMaxValue}">
    <ItemsControl Grid.Row="0" wpflib:RatioContentPresenter2.RatioOffset="{Binding Path=RatioOffset}" wpflib:RatioContentPresenter2.RatioValue="{Binding Path=RatioValue}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                    <wpflib:RatioPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Rectangle x:Name="RatioShape" wpflib:RatioPanel.Ratio="{Binding Path=Value}" Fill="{Binding Path=Brush}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemsSource>
            <Binding  Path="RatioItems" Mode="OneWay" />
        </ItemsControl.ItemsSource>
    </ItemsControl>
</wpflib:RatioContentPresenter2>

推荐答案

RatioPanel的子级将是ContentPresenter的实例,假设这些项不是UIElements. ContentPresenter将显示您在ItemTemplate中定义的DataTemplate.

The children of the RatioPanel will be instances of ContentPresenter, assuming the items are not UIElements. The ContentPresenter will display the DataTemplate you have defined in ItemTemplate.

通常,面板直接与孩子一起工作.您正在ContentPresenter的子级(您的面板的子级)上设置附加属性.我相信您应该直接在ContentPresenter上进行设置.像这样:

Normally, panels work directly with their children. You are setting your attached property on the a child of the ContentPresenter, which is a child of your panel. I believe you should be setting this on the ContentPresenter directly. So something like this:

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="wpflib:RatioPanel.Ratio" Value="{Binding Path=Value}" />
    </Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Rectangle x:Name="RatioShape" Fill="{Binding Path=Brush}" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

这篇关于使用自定义面板问题将附加属性绑定到ItemsControl中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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