Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中的视图模型上应用模板样式_C#_Wpf_Xaml_Mvvm_Binding - Fatal编程技术网

C# 在WPF中的视图模型上应用模板样式

C# 在WPF中的视图模型上应用模板样式,c#,wpf,xaml,mvvm,binding,C#,Wpf,Xaml,Mvvm,Binding,我正在开发一个WPF应用程序,该应用程序包含一个设计器,它显示不同元素(形状)的垂直列表视图 我为设计师创建了一个视图模型,并为每个形状创建了视图模型。为了将设计器的视图绑定到视图模型,我使用了“DataContext”属性 但我的问题是,我在一个XAML文件中定义了形状的所有视图样式(模板),我不知道如何将它们绑定到视图模型 我在网上找到了这个: var resourceDictionary = new ResourceDictionary() { So

我正在开发一个WPF应用程序,该应用程序包含一个设计器,它显示不同元素(形状)的垂直列表视图

我为设计师创建了一个视图模型,并为每个形状创建了视图模型。为了将设计器的视图绑定到视图模型,我使用了“DataContext”属性

但我的问题是,我在一个XAML文件中定义了形状的所有视图样式(模板),我不知道如何将它们绑定到视图模型

我在网上找到了这个:

var resourceDictionary = new ResourceDictionary()
        {
            Source = new Uri("SymbolTemplates.xaml", UriKind.Relative)
        };

Style template = resourceDictionary["SMS"] as Style;
所以我把它放在我的视图模型构造函数中,但是我和feild“template”有什么关系呢

为了让事情更清楚:

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>

“儿童列表”包含我的形状视图模型列表

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(名为“ShapeViewer”)中定义的“ChildrenList”中添加此“CircleStyle”时,如何将其分配给我的“CircleVM”?更新>>

要解决您用粗体表示的问题,请执行以下操作:

你的
风格
并不像你说的那样是“为了一个圆”。。。它用于
列表视图项
。您不能使用
TargetType=“ListViewItem”
样式设置为
UserControl

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

然后,您可以将
DataTemplate
与特定的
ContentControl
连接起来,如下所示:

ComponentResourceKey templateKey = new ComponentResourceKey(typeof(YourViewModel), 
"YourViewModelDataTemplate");
DataTemplate dataTemplate = (DataTemplate)this.TryFindResource(templateKey);
contentControl.ContentTemplate = dataTemplate;
DataTemplate dataTemplate = new DataTemplate();
dataTemplate.DataType = typeof(YourViewModel);
FrameworkElementFactory view = new FrameworkElementFactory(typeof(YourView));
dataTemplate.VisualTree = view;
这假设您有一个名为
ContentControl
ContentControl

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

ComponentResourceKey templateKey = new ComponentResourceKey(typeof(YourViewModel), 
"YourViewModelDataTemplate");
DataTemplate dataTemplate = (DataTemplate)this.TryFindResource(templateKey);
contentControl.ContentTemplate = 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>

...

您能给我们展示一下您的观点吗?您可以在其中处理形状的数据模板。谢谢您的重播,当然,这是我的ListView:ChildrenList包含我的形状视图模型列表。谢谢您的回答,我应该在哪里写这个?在我的形状视图模型中?我的形状视图只是XAML文件中定义的“样式”,因此我认为在创建样式时必须加载样式“a”,并将其分配给视图模型“a_vm”。这是真的吗?