我在ViewModel中有集合,在集合中我有属性Name和Color。我需要将color属性绑定到模板fill.x:key =“model”是为ViewModel
在C#中:
public class Model
{
public string Name;
public Color Color;
}
public ViewModel
{
public ObservableCollection<Model> Models { get; set; }
public MapViewModel()
{
this.Models = new ObservableCollection<Model>();
this.Models.Add(new Model() { Name = "Prabhu",Color = Colors.Red });
this.Models.Add(new Model() { Name = "raji",Color = Colors.Green });
this.Models.Add(new Model() { Name = "Pranov",Color = Colors.Black });
}
}
在XAML中:
<DataTemplate>
<Path Data="F1M1433.97,-17.992L1401.97,-49.992 1433.97,-81.992 1465.97,-49.992 1433.97,-17.992z" Fill="{Binding Data.model }/>
</DataTemplate>
是否需要使用转换器?
答案 0 :(得分:0)
Fill
属性的类型为Brush
,而不是Color
。您想要的是将Model.Color
属性类型更改为Brush
或指定SolidColorBrush
:
<Path ...>
<Path.Fill>
<SolidColorBrush Color="{Binding Color}"/>
</Path.Fill>
</Path>
还要确保使用属性,而不是字段:
public class Model
{
public string Name { get; set; }
public Color Color { get; set; }
}
答案 1 :(得分:-1)
<Path Data="F1M1433.97,-17.992L1401.97,-49.992 1433.97,-81.992 1465.97,-49.992 1433.97,-17.992z" Fill="{Binding Data.Color }/>
当Color绑定到Fill
时,它可以正常工作