简体   繁体   English

如何为ItemsControl中的每个项目显示某个路径?

[英]How to display a certain Path for each item in an ItemsControl?

So I have an ItemsControl, and for each item I would like to render a System.Windows.Shapes.Path to indicate the item's status. 所以我有一个ItemsControl,对于每个项目,我想渲染一个System.Windows.Shapes.Path来指示项目的状态。 Each status should use a different color for the Path. 每个状态应使用不同的路径颜色。

What I'm currently doing (which works, but is a horrible solution), is adding a property for each status to the view model, for example, ShowIconA , ShowIconB , ShowIconC , and a Path for each status to the data template: 我目前正在做什么(哪个有效但是是一个可怕的解决方案),是为视图模型添加每个状态的属性,例如, ShowIconAShowIconBShowIconC以及数据模板的每个状态的路径:

<DataTemplate>
    <Path Fill="Red" Visibility="{Binding ShowIconA, Converter={StaticResource BoolVisConv}}" Data="..." />
    <Path Fill="Green" Visibility="{Binding ShowIconB, Converter={StaticResource BoolVisConv}}" Data="..." />
    <Path Fill="Blue" Visibility="{Binding ShowIconC, Converter={StaticResource BoolVisConv}}" Data="..." />
    ...
</DataTemplate>

What's a better way to handle this? 有什么更好的方法来处理这个问题? I'd like to just have a status enum and then look up a Path or something in a dictionary, and I'd also like to avoid building the Path through code. 我想要一个状态枚举,然后在字典中查找路径或其他东西,我也想避免通过代码构建路径。

Another thought I had was to use a converter like the following, and then I could bind an Image's Source property to the status property. 我的另一个想法是使用如下的转换器,然后我可以将Image的Source属性绑定到status属性。 The problem with this approach was that I couldn't find a good way to size the paths. 这种方法的问题是我找不到一个很好的方法来调整路径。 I would need to either calculate the appropriate relative scaling transform for each one, or set the dimensions on the image itself, which would just do bitmap scaling. 我需要为每个计算适当的相对缩放变换,或者在图像本身上设置尺寸,这只会进行位图缩放。

<conv:DictionaryConverter x:Key="StatusIcons">
    <DrawingImage x:Key="Status1">
        <DrawingImage.Drawing>
            <GeometryDrawing Brush="Red" Geometry="...">
        </DrawingImage.Drawing>
    </DrawingImage>
    <DrawingImage x:Key="Status2">
        <DrawingImage.Drawing>
            <GeometryDrawing Brush="Green" Geometry="...">
        </DrawingImage.Drawing>
    </DrawingImage>
</conv:DictionaryConverter>

I also tried putting Paths into a converter like the above and then binding a ContentControl in the DataTemplate, but that seems to cause issues since there's only one Path instance for each status, and I'm trying to use them in multiple places? 我也尝试将Paths放入如上所述的转换器中,然后在DataTemplate中绑定ContentControl,但这似乎会导致问题,因为每个状态只有一个Path实例,我试图在多个地方使用它们?

I suppose I could have one dictionary of strings for the path data, and another for the colors, and a converter for each, but that still doesn't seem great. 我想我可以为路径数据设置一个字符串字典,为颜色设置另一个字典,并为每个设置一个转换器,但这似乎仍然不太好。

Any thoughts? 有什么想法吗?

You can implement is like below... 你可以实现如下...

<conv:StatusFillConverter x:Key="statusFillConverter"></conv:StatusFillConverter>
<conv:StatusDataConverter x:Key="statusDataConverter"></conv:StatusDataConverter >

<Path Fill="{Binding Status, Converter={StaticResource statusFillConverter}}" Data="{Binding Status, Converter={StaticResource statusDataConverter}}"></Path>

How about using a single property on your viewmodel to indicate which path you want to draw? 如何在viewmodel上使用单个属性来指示要绘制的路径?

The trick here is that instead of making it an enum, you make it a class, so; 这里的诀窍是,不要把它变成一个枚举,而是让它成为一个类,所以;

public class MyViewModel
{
   public object PathType; //or you could have a base class/interface
  ...
}

public class Status1Path
{
   // Don't need an implementation, just its type
}

public class Status2Path
{
   // Don't need an implementation, just its type
}

Now all you do is use a DataTemplate for each class to draw the different imagery 现在,您只需为每个类使用DataTemplate来绘制不同的图像

<DataTemplate DataType="{x:Type Status1Path}">
  <Path Fill="Red" Data="..." />
</DataTemplate>    

<DataTemplate DataType="{x:Type Status2Path}">
  <Path Fill="Green" Data="..." />
</DataTemplate>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM