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

时间:2023-02-06 13:21:11

A have a ListBox of items, every ListBoxItem contains an icon in the form of a Path object, like so:

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>

The Path's style is contained in Appl.xaml (Application.Resources section) and is the following:

Path的样式包含在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>

The trouble is that only the first item in the ListBox binds the Data property as expected, the other ones don't bind it at all (hence they appear as blank space, but match the size of the Path). Also when I use the style anywhere else (i.e. outside the ListBox), only the first instance that occurs will bind.

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

The weird thing is that if I define for example the Fill property in the Style instead of inline, it works just fine and doesn't exibit the same problems as the Path property.

奇怪的是,如果我在Style中定义了Fill属性而不是内联,它可以正常工作并且不会出现与Path属性相同的问题。

My guess is that is has something to do with Data not being a primitive type, but I haven't found any fixes.

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

EDIT: Interestingly, when I bind the Data property directly to System.String resource, it works. I would still like to be able to define this property via a Style though.

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

EDIT 2: I've just came across the same issue in WPF, when setting Path to a Content of a Button via a Style that is used across more buttons. The path shows up in just one buttons, the others are blank.

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

5 个解决方案

#1


5  

Path.Fill is a DependencyProperty, while Path.Data isn't. Instead do:

Path.Fill是DependencyProperty,而Path.Data则不是。相反:

<DataTemplate>
    <Grid ...>
        ...
        <ContentPresenter Content="{StaticResource MyPath}"/>
    </Grid>
</DataTemplate>

ContentPresenter.Content is a DependencyProperty so this should work:

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

#2


4  

I am guessing that Geometry cannot be shared. Have you tried setting the x:Shared= "false" to:

我猜不能分享几何。您是否尝试将x:Shared =“false”设置为:

<Style x:Key="ErrorIconPath" TargetType="Path">

#3


3  

I've experienced the same behavior in Silverlight and asked a similar question here on *.com ( https://*.com/q/13426198/1796930), but as I'm writing this, it's been 1 month and I've yet to get even a single answer.

我在Silverlight中遇到过相同的行为,并在*.com(https://*.com/q/13426198/1796930)上提出了类似的问题,但正如我写的那样,已经过了1个月了,我还没有得到一个答案。

However, as you mentioned in your first edit, I too was able to perform a workaround by creating a resource with my geometry data as a string and then binding the Data property of the Path objects to the string resource resource.

但是,正如您在第一次编辑中提到的那样,我也可以通过创建一个将我的几何数据作为字符串创建资源,然后将Path对象的Data属性绑定到字符串资源资源来执行变通方法。

I also had to create two instances of the Path objects that were identical other than each one using a different resource (i.e. two different icons) and then binding the visibility of each to a property in my ViewModel to display the appropriate one.

我还必须使用不同的资源(即两个不同的图标)创建两个相同的Path对象实例,然后将每个实例的可见性绑定到我的ViewModel中的属性以显示适当的一个。

#4


2  

I am very sure that you did not forgot the stroke here in Path style

我很确定你没有忘记路径风格的笔画

<Setter Property="Stroke" Value="Red"/>

I have tested you code on my machine , it worked fine if above line added in style

我已经在我的机器上测试了你的代码,如果上面的行添加了风格,它工作得很好

#5


1  

My first tought was your Path would be broken or not valid. But then I saw you are using the Syncfusion Metro Studio. I tried it with exactly the same code you have and it worked very well. In a Data Template of 5 Items or as a single Path Item.

我的第一次尝试是你的路径将被破坏或无效。但后来我看到你正在使用Syncfusion Metro Studio。我用完全相同的代码尝试了它并且它工作得非常好。在5个项目的数据模板中或作为单个路径项目。

Have you tried to set the Fill statically to Red or something?

您是否尝试将静音设置为红色或其他?

Also maybe try this for the Style definition

也可以尝试使用Style定义

<Style x:Key="ErrorIconPath" TargetType="{x:Type Path}">

Third suggestion would be to move the style definition from the App to your Page or even to your Control itself.

第三个建议是将样式定义从应用程序移动到您的页面,甚至移动到您的控件本身。

To be sure there will be no default styles applied, try

为确保没有应用默认样式,请尝试

OverridesDefaultStyle="True"

Hope this helps :)

希望这可以帮助 :)

#1


5  

Path.Fill is a DependencyProperty, while Path.Data isn't. Instead do:

Path.Fill是DependencyProperty,而Path.Data则不是。相反:

<DataTemplate>
    <Grid ...>
        ...
        <ContentPresenter Content="{StaticResource MyPath}"/>
    </Grid>
</DataTemplate>

ContentPresenter.Content is a DependencyProperty so this should work:

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

#2


4  

I am guessing that Geometry cannot be shared. Have you tried setting the x:Shared= "false" to:

我猜不能分享几何。您是否尝试将x:Shared =“false”设置为:

<Style x:Key="ErrorIconPath" TargetType="Path">

#3


3  

I've experienced the same behavior in Silverlight and asked a similar question here on *.com ( https://*.com/q/13426198/1796930), but as I'm writing this, it's been 1 month and I've yet to get even a single answer.

我在Silverlight中遇到过相同的行为,并在*.com(https://*.com/q/13426198/1796930)上提出了类似的问题,但正如我写的那样,已经过了1个月了,我还没有得到一个答案。

However, as you mentioned in your first edit, I too was able to perform a workaround by creating a resource with my geometry data as a string and then binding the Data property of the Path objects to the string resource resource.

但是,正如您在第一次编辑中提到的那样,我也可以通过创建一个将我的几何数据作为字符串创建资源,然后将Path对象的Data属性绑定到字符串资源资源来执行变通方法。

I also had to create two instances of the Path objects that were identical other than each one using a different resource (i.e. two different icons) and then binding the visibility of each to a property in my ViewModel to display the appropriate one.

我还必须使用不同的资源(即两个不同的图标)创建两个相同的Path对象实例,然后将每个实例的可见性绑定到我的ViewModel中的属性以显示适当的一个。

#4


2  

I am very sure that you did not forgot the stroke here in Path style

我很确定你没有忘记路径风格的笔画

<Setter Property="Stroke" Value="Red"/>

I have tested you code on my machine , it worked fine if above line added in style

我已经在我的机器上测试了你的代码,如果上面的行添加了风格,它工作得很好

#5


1  

My first tought was your Path would be broken or not valid. But then I saw you are using the Syncfusion Metro Studio. I tried it with exactly the same code you have and it worked very well. In a Data Template of 5 Items or as a single Path Item.

我的第一次尝试是你的路径将被破坏或无效。但后来我看到你正在使用Syncfusion Metro Studio。我用完全相同的代码尝试了它并且它工作得非常好。在5个项目的数据模板中或作为单个路径项目。

Have you tried to set the Fill statically to Red or something?

您是否尝试将静音设置为红色或其他?

Also maybe try this for the Style definition

也可以尝试使用Style定义

<Style x:Key="ErrorIconPath" TargetType="{x:Type Path}">

Third suggestion would be to move the style definition from the App to your Page or even to your Control itself.

第三个建议是将样式定义从应用程序移动到您的页面,甚至移动到您的控件本身。

To be sure there will be no default styles applied, try

为确保没有应用默认样式,请尝试

OverridesDefaultStyle="True"

Hope this helps :)

希望这可以帮助 :)