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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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# &引用;占位符;静态资源中的绑定_C#_Wpf - Fatal编程技术网

C# &引用;占位符;静态资源中的绑定

C# &引用;占位符;静态资源中的绑定,c#,wpf,C#,Wpf,我有一个列表视图,它从静态资源获取其样式。例如,我在我的MainWindowViewModel中有一个自定义对象的ObservableCollection。此对象包含一些属性,包括MyCustomObjectProperty。MainWindowViewModel还有一个ICommand,MyCommand 我的风格(为简单起见,裁剪了一些片段): 这个代码有效。单击列表视图中的按钮时,MyCommandOne将使用单击的列表视图项表示的自定义对象中的参数执行 我的问题是:有没有办法用某种占位符

我有一个
列表视图
,它从静态资源获取其
样式
。例如,我在我的
MainWindowViewModel
中有一个自定义对象的
ObservableCollection
。此对象包含一些属性,包括
MyCustomObjectProperty
MainWindowViewModel
还有一个
ICommand
MyCommand

我的风格(为简单起见,裁剪了一些片段):

这个代码有效。单击
列表视图
中的按钮时,
MyCommandOne
将使用单击的列表视图项表示的自定义对象中的参数执行


我的问题是:有没有办法用某种占位符替换
DataContext.MyCommandOne,RelativeSource…
,这样我就可以在实际的
列表视图的标记中指定所需的命令?这样,我可以用这种样式创建更多的
ListView
s,但执行不同的命令。

解决方法-将
ListView
标记设置为实际命令,然后从
按钮
绑定到
ListView
标记
属性

<DataTemplate>
   <Button Command="{Binding Tag, RelativeSource={RelativeSource FindAncestor,
                                  AncestorType=ListView}}"/>
    ........
</DataTemplate>
<ListView Grid.Column="1"
                  Grid.Row="1"
                  ItemsSource="{Binding Path=MyObservableCollection}"
                  Style="{StaticResource MyListViewStyle}"
                  Margin="5"
                  BorderThickness="0"
                  Background="LightGray"
                  />
<DataTemplate>
   <Button Command="{Binding Tag, RelativeSource={RelativeSource FindAncestor,
                                  AncestorType=ListView}}"/>
    ........
</DataTemplate>
<ListView Tag="{Binding MyCommandOne}"/>