如何从 WPF 样式传递当前 DataGridCell 信息
How to Pass Current DataGridCell Information from WPF Style
我的样式为 TargetType=DataGridCell,它包含 ContextMenu。我需要通过 CommandParameter 将 DataGridCell 信息传递给该命令。
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellStyle">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Copy" Command="{Binding CopyToClipBoardCommand, Mode=OneWay}" CommandParameter={}>
<MenuItem.DataContext>
<cust:CopytoContext/>
</MenuItem.DataContext>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
xmlns:cust="clr-namespace:MyProject.Util"
Class源代码
public class CopytoContext
{
#region Commands
public ICommand CopyToClipBoardCommand
{
get
{
return new DelegatingCommand((object param) =>
{
new Action(() =>
{
/// Logic to Copy the Content to Clipboard
}).Invoke();
});
}
}
#endregion
}
如何将当前 DataGridCell 信息传递给该命令。请协助我...
如果您不想为控件使用名称,那么您可以使用 RelativeSource 绑定:
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Copy" Command="{Binding CopyToClipBoardCommand, Mode=OneWay}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget}">
<MenuItem.DataContext>
<cust:CopytoContext />
</MenuItem.DataContext>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
ContextMenu
returns ContextMenu
附加的控件的 PlacementTarget
属性 ( 单元格 在这种情况下)。
然后在你的命令中你可以使用这个代码:
DataGridCell dataGridCell = param as DataGridCell;
if (dataGridCell != null)
{
TextBlock textBlock = dataGridCell.Content as TextBlock;
if (textBlock != null)
{
Clipboard.SetText(textBlock.Text);
}
}
希望对您有所帮助
我的样式为 TargetType=DataGridCell,它包含 ContextMenu。我需要通过 CommandParameter 将 DataGridCell 信息传递给该命令。
<Style TargetType="{x:Type DataGridCell}" x:Key="DataGridCellStyle">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Copy" Command="{Binding CopyToClipBoardCommand, Mode=OneWay}" CommandParameter={}>
<MenuItem.DataContext>
<cust:CopytoContext/>
</MenuItem.DataContext>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
xmlns:cust="clr-namespace:MyProject.Util"
Class源代码
public class CopytoContext
{
#region Commands
public ICommand CopyToClipBoardCommand
{
get
{
return new DelegatingCommand((object param) =>
{
new Action(() =>
{
/// Logic to Copy the Content to Clipboard
}).Invoke();
});
}
}
#endregion
}
如何将当前 DataGridCell 信息传递给该命令。请协助我...
如果您不想为控件使用名称,那么您可以使用 RelativeSource 绑定:
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Copy" Command="{Binding CopyToClipBoardCommand, Mode=OneWay}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget}">
<MenuItem.DataContext>
<cust:CopytoContext />
</MenuItem.DataContext>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
ContextMenu
returns ContextMenu
附加的控件的 PlacementTarget
属性 ( 单元格 在这种情况下)。
然后在你的命令中你可以使用这个代码:
DataGridCell dataGridCell = param as DataGridCell;
if (dataGridCell != null)
{
TextBlock textBlock = dataGridCell.Content as TextBlock;
if (textBlock != null)
{
Clipboard.SetText(textBlock.Text);
}
}
希望对您有所帮助