【发布时间】:2019-10-11 18:30:25
【问题描述】:
有一个 WPF DataGrid 具有在 Ctrl+MouseWheel 事件上动态更改 FontSize 的功能。
它是使用 Caliburn 的 cal:Message.Attach 功能实现的:
<!-- xaml -->
cal:Message.Attach="[Event PreviewMouseWheel] = [Action ChangeDataGridFont($eventArgs)]"
C# 视图模型:
public void ChangeDataGridFont(MouseWheelEventArgs args)
{
if (Keyboard.Modifiers == ModifierKeys.Control && args != null)
{
DataGridFontSize += Math.Sign(args.Delta) * 2;
args.Handled = true;
}
}
DataGridFontSize 是绑定到CellStyle 的DataGrid 的VM 属性。
有没有办法克服这种影响(如果可能,在 MvvM 的范围内)?
更新:看起来我们在更改FontSize 时需要手动更新列宽。我可以在相应的样式中绑定列的宽度:
<Style x:Key="IdCellStyle" TargetType="{x:Type DataGridCell}" >
<Setter Property="Width" Value="{Binding DataContext.ColWidth, Mode=TwoWay,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
....
</Style>
并定义属性:
public double ColWidth
{
get => _colWidth;
set
{
_colWidth = value;
NotifyOfPropertyChange();
}
}
我应该在DataGridFontSize setter 中将ColWidth 设置为什么?
【问题讨论】: