标签:dynamic 方法 class panel sys .sh owb stat binding
直接上代码,首先是一个ContextMenu的模板:
<ContextMenu x:Key="Menu" BorderThickness="0.3" FontSize="13" DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}"> <MenuItem Header="复制" Command="{Binding Source={x:Static vm:ViewModelLocator.Instance},Path=MainWindowVM.CopyImageCommand}" CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" /> <MenuItem Header="另存为..." Command="{Binding Source={x:Static vm:ViewModelLocator.Instance},Path=MainWindowVM.SaveImageCommand}" CommandParameter="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandTarget="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" /> </ContextMenu>
然后是DataTemplate的内容:
<DataTemplate x:Key="CustomerImageDataTemplate"> <Button Command="{Binding DataContext.ShowImageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Cursor="Hand" CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,AncestorType=StackPanel}}" Padding="3"> <Button.Template> <ControlTemplate TargetType="Button"> <Grid> <Border Name="imgBorder" BorderBrush="{StaticResource ListBorder}" CornerRadius="3" BorderThickness="0" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" Margin="0"> </Border> <Image x:Name="ImageMsg" Source="{Binding TextContent}" Stretch="Uniform" Width="120" Height="80" ContextMenu="{StaticResource Menu}" Tag="{Binding}"> <Image.OpacityMask> <VisualBrush Visual="{Binding ElementName=imgBorder, Mode=OneTime}" /> </Image.OpacityMask> </Image> </Grid> </ControlTemplate> </Button.Template> </Button> </DataTemplate>
上面的Button还展示了将图片四角设置为圆角的方法。
[WPF]解决模板中ContextMenu绑定CommandParameter的问题
标签:dynamic 方法 class panel sys .sh owb stat binding
原文地址:http://www.cnblogs.com/lionwang/p/7491685.html