wpf - Path.Data 样式仅适用于样式对象的第一个实例

标签 wpf windows-phone-7 xaml styles

A 有一个 ListBox 项目,每个 ListBoxItem 都包含一个 Path 形式的图标对象,像这样:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid ...>
            ...
            <Path Margin="4" Style="{StaticResource ErrorIconPath}" 
                  Stretch="Uniform" Width="26" Height="26"
                  RenderTransformOrigin="0.5,0.5" Grid.Column="1" Grid.Row="1"
                  UseLayoutRounding="False"
                  HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

路径的样式包含在 Appl.xaml ( Application.Resources 部分)并且如下:
<Style x:Key="ErrorIconPath" TargetType="Path">
    <Setter Property="Data" Value="F1M874.094,289.369L854.3,254.63C854.028,254.151 853.515,253.856 852.958,253.856 852.403,253.856 851.89,254.151 851.617,254.63L831.824,289.369C831.555,289.84 831.559,290.416 831.835,290.883 832.111,291.348 832.618,291.634 833.165,291.634L872.752,291.634C873.299,291.634 873.805,291.348 874.081,290.883 874.357,290.416 874.361,289.84 874.094,289.369 M855.653,287.189L850.264,287.189 850.264,282.745 855.653,282.745 855.653,287.189z M855.653,279.41L850.264,279.41 850.264,266.077 855.653,266.077 855.653,279.41z" />
</Style>

问题是只有 ListBox 中的第一项绑定(bind)了 Data正如预期的那样,其他属性根本不绑定(bind)它(因此它们显示为空白,但与 Path 的大小匹配)。此外,当我在其他任何地方(即在 ListBox 之外)使用该样式时,只有发生的第一个实例会绑定(bind)。

奇怪的是,如果我定义例如 Fill Style 中的属性(property)而不是内联,它工作得很好,不会出现与 Path 相同的问题属性(property)。

我的猜测是这与 Data 有关不是原始类型,但我还没有找到任何修复。

编辑:有趣的是,当我绑定(bind) Data属性(property)直接到System.String资源,它的工作原理。我仍然希望能够通过 Style 定义这个属性。

编辑 2:我刚刚在 WPF 中遇到了同样的问题,当通过在更多按钮中使用的样式将路径设置为按钮的内容时。路径仅显示在一个按钮中,其他按钮为空白。

最佳答案

Path.Fill 是一个 DependencyProperty,而 Path.Data 不是。而是这样做:

<DataTemplate>
    <Grid ...>
        ...
        <ContentPresenter Content="{StaticResource MyPath}"/>
    </Grid>
</DataTemplate>
ContentPresenter.Content是一个 DependencyProperty 所以这应该工作:
<Path x:Key="MyPath" Margin="4" Style="{StaticResource ErrorIconPath}" 
      Stretch="Uniform" Width="26" Height="26" VerticalAlignment="Center"
      RenderTransformOrigin="0.5,0.5" Grid.Column="1" Grid.Row="1"
      UseLayoutRounding="False" HorizontalAlignment="Center"
      Data="F1M874.094,289.369L854.3,254.63C854.028,254.151 853.515,253.856 852.958,253.856 852.403,253.856 851.89,254.151 851.617,254.63L831.824,289.369C831.555,289.84 831.559,290.416 831.835,290.883 832.111,291.348 832.618,291.634 833.165,291.634L872.752,291.634C873.299,291.634 873.805,291.348 874.081,290.883 874.357,290.416 874.361,289.84 874.094,289.369 M855.653,287.189L850.264,287.189 850.264,282.745 855.653,282.745 855.653,287.189z M855.653,279.41L850.264,279.41 850.264,266.077 855.653,266.077 855.653,279.41z"/>

关于wpf - Path.Data 样式仅适用于样式对象的第一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13702171/

相关文章:

c# - WPF 窗口不可点击

c# - Windows Phone 7 和 MP4

c# - XAML - ListViewItem - IsEnabled 绑定(bind)

xaml - 将PopUp放置在Itemscontrol中

c# - 创建的 WPF 应用程序无法启动

c# - 为什么 IsFocused 没有在 Label 上触发

c# - 在 Silverlight Mobile 中实现 DispatchTimer

silverlight - 如何在 Windows Phone 7 中禁用 ListBoxes/ScrollViewer 的过度滚动效果

适用于 Windows(桌面)和 Windows Phone 8 的 C# MVVM 应用程序框架

.net - Avalon 文本编辑器中上下键的光标位置没有改变