使用自定义面板问题将附加属性绑定到ItemsControl中的项目

时间:2023-01-19 09:07:54

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.

我无法按照自己的意愿使用以下XAML。所有绑定都有效,因为绑定没有错误。但是我没有从RatioShape矩形的绑定中得到预期的结果。问题是附加属性wpflib:RatioPanel.Ratio始终返回其默认值,而不是数据绑定值。

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?

所以我认为RatioShape上的附加属性设置在错误的“上下文”中。如何绑定到附加属性,以便wpflib:RatioPanel获取绑定值?

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

1 个解决方案

#1


6  

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.

如果项目不是UIElements,则RatioPanel的子项将是ContentPresenter的实例。 ContentPresenter将显示您在ItemTemplate中定义的DataTemplate。

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:

通常,面板直接与孩子一起工作。您正在ContentPresenter的子项上设置附加属性,该子项是您的面板的子项。我相信你应该直接在ContentPresenter上设置它。所以这样的事情:

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

#1


6  

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.

如果项目不是UIElements,则RatioPanel的子项将是ContentPresenter的实例。 ContentPresenter将显示您在ItemTemplate中定义的DataTemplate。

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:

通常,面板直接与孩子一起工作。您正在ContentPresenter的子项上设置附加属性,该子项是您的面板的子项。我相信你应该直接在ContentPresenter上设置它。所以这样的事情:

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