将附加属性绑定到具有自定义面板问题的 ItemsControl 中的项目
我无法让以下 XAML 按我想要的方式工作。所有绑定都有效,因为绑定中没有错误。但我没有从 RatioShape 矩形上的绑定得到预期的结果。问题是附加属性 wpflib:RatioPanel.Ratio 始终返回其默认值,而不是数据绑定值。
所以我认为 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>
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.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设项不是 UIElement,RatioPanel 的子项将是 ContentPresenter 的实例。 ContentPresenter 将显示您在 ItemTemplate 中定义的 DataTemplate。
通常,小组直接与他们的孩子一起工作。您正在 ContentPresenter 的子级(面板的子级)上设置附加属性。我相信您应该直接在 ContentPresenter 上进行设置。所以像这样:
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.
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: