我正在开发一个 WPF 应用程序,该应用程序包含一个设计器,它显示不同元素(形状)的垂直 ListView。
我为设计师创建了一个视图模型,并为每个形状创建了视图模型。为了将设计器的视图绑定到视图模型,我使用了“DataContext”属性。
但我的问题是我在一个 XAML 文件中定义了形状的所有视图样式(模板),我不知道如何将它们绑定到它们的视图模型!
我在网上找到了这个:
var resourceDictionary = new ResourceDictionary()
{
Source = new Uri("SymbolTemplates.xaml", UriKind.Relative)
};
Style template = resourceDictionary["SMS"] as Style;
所以我把它放在我的视图模型构造函数中,但是我与字段“模板”有什么关系?
为了让事情更清楚:
1)这是我的设计师观点:
<Grid SizeChanged="Grid_SizeChanged">
<ListView x:Name="ShapesViewer" BorderThickness="0" Background="YellowGreen" ItemsSource="{Binding ChildrenList}">
<ListView.LayoutTransform>
<RotateTransform Angle="{Binding Orientation}" />
</ListView.LayoutTransform>
</ListView>
</Grid>
“ChildrenList”包含我的形状视图模型列表。
2)这是我的“SymbolTemplates.xaml”,我在其中定义了所有形状样式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mvvm="clr-namespace:ViewModel;assembly=ViewModel">
<Style x:Key="CircleStyle"
TargetType="ListViewItem">
<Setter Property="Visibility" Value="Visible"/>
<!--Value="{Binding IsExpanded, Mode=TwoWay}" />-->
<Setter Property="Width"
Value="70" />
<Setter Property="Height"
Value="32" />
<Setter Property="Margin"
Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid Height="32"
Width="50"
Background="Transparent">
<Grid.HorizontalAlignment>
<MultiBinding Converter="{StaticResource EvenToHorizontalAlignementMultiConverter}">
<Binding Path="Position" />
<Binding Path="RenderCenter" />
</MultiBinding>
</Grid.HorizontalAlignment>
<Ellipse Width="30"
Height="30"
HorizontalAlignment="Center"
VerticalAlignment="Center"
StrokeThickness="3"
Fill="WhiteSmoke">
</Ellipse>
<ItemsPresenter Grid.Row="1"
Visibility="{Binding IsExpanded, Converter= {StaticResource VisibilityOfBool} }" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
...
所以,我定义了这种风格(对于一个圆圈),我还有一个对象 CircleVM(视图模型)。
我的问题是:在我的 ListView(名为“ShapesViewer”)中定义的“ChildrenList”中添加这个“CircleStyle”时,我应该怎么做才能将它分配给我的“CircleVM”?