1

我正在开发一个 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”?

4

1 回答 1

3

更新>>>

要解决您以粗体表示的声明:

正如您所说,您Style不是“为了一个圈子”……它是为了一个ListViewItem. 您不能将 a Stylewith设置TargetType="ListViewItem"为 a UserControl

不要这样做,而是DataTemplate为每个视图定义一个,定义您希望每个视图模型中的数据如何显示...例如(假设您的Circle类中有一个公共Name属性):

<DataTemplate DataType="{x:Type YourDataTypesNamespace:Circle}">
    <Grid Height="32" Width="50" Background="Transparent">
        <Ellipse Width="30" Height="30" HorizontalAlignment="Center" 
VerticalAlignment="Center" StrokeThickness="3" Fill="WhiteSmoke" />
        <TextBlock Text="{Binding Name}" />
    </Grid>
</DataTemplate>

更新结束>>>

DataTemplate首先,您可以像这样从您的Resources部分加载一个:

ComponentResourceKey templateKey = new ComponentResourceKey(typeof(YourViewModel), 
"YourViewModelDataTemplate");
DataTemplate dataTemplate = (DataTemplate)this.TryFindResource(templateKey);

然后你可以DataTemplate用这样的特定连接ContentControl

contentControl.ContentTemplate = dataTemplate;

这假设您有一个ContentControl名为contentControl.

或者,您可以创建一个简单DataTemplate的以编程方式将每个视图与其相关的视图模型连接起来,如下所示:

DataTemplate dataTemplate = new DataTemplate();
dataTemplate.DataType = typeof(YourViewModel);
FrameworkElementFactory view = new FrameworkElementFactory(typeof(YourView));
dataTemplate.VisualTree = view;

更新>>>

但是,如果您UserControl的每个视图模型都有一个视图 (),那么有一种简单的方法可以连接它们……将这些添加到App.xaml

<DataTemplate DataType="{x:Type ViewModels:CircleViewModel}">
    <Views:CircleView />
</DataTemplate>
...
<DataTemplate DataType="{x:Type ViewModels:SquareViewModel}">
    <Views:SquareView />
</DataTemplate>
于 2013-08-28T11:25:28.477 回答