如何解决带有自定义面板问题的绑定属性绑定到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>
解决方法
假设项目不是UIElements,RatioPanel的子级将是ContentPresenter的实例。 ContentPresenter将显示您在ItemTemplate中定义的DataTemplate。
通常,小组直接与孩子一起工作。您正在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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。