【问题标题】:How can I change a cell of a DataGrid depending on the data it is bound to using MVVM?如何根据使用 MVVM 绑定的数据更改 DataGrid 的单元格?
【发布时间】:2011-11-22 22:47:20
【问题描述】:

我正在使用 MVVM Light Toolkit,并且我有一个绑定到 ObservableCollection 的 DataGrid。仅显示一个文本列。我希望单元格的文本为粗体或正常,具体取决于显示的对象内部的布尔值。我想我可以使用 RelayCommands,但它们只需要 1 个参数,我需要至少 2 个参数来获取 CellContent(DataGridRowEventArgs 和 DataGrid 本身)。我试图在“LoadingRow”事件上触发 RelayCommand Execute 委托,但只有一个参数我做不到。

这是 XAML 中的 DataGrid:

<DataGrid x:Name="dataGrid1" HorizontalAlignment="Left" Margin="112,34,0,8" Width="100" IsReadOnly="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" ItemsSource="{Binding CurrentNewsList}" AutoGenerateColumns="False" SelectedIndex="0">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Title}" MinWidth="92" Width="Auto" FontFamily="Segoe UI" Foreground="Black" FontWeight="{Binding CurrentNewsList[0].MyFont}"/>
        </DataGrid.Columns>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <Custom:EventToCommand Command="{Binding NewsSelectedCommand}" CommandParameter="{Binding SelectedIndex, ElementName=dataGrid1}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>

我在 Blend 中设置了网格。请注意,FontWeight 的绑定类似于“{Binding CurrentNewsList[0].MyFont}”。这样对吗 ?我也尝试了“{Binding MyFont}”,但都得到了相同的结果:No BOld :(

MyFont 在 Object 构造函数中设置为布尔值:

MyFont = newIsRead ? FontWeights.Normal : FontWeights.Bold;

请帮忙。

谢谢

【问题讨论】:

  • 布尔值是否改变,需要更新字体样式,还是总是粗体/正常?

标签: c# wpf xaml datagrid mvvm-light


【解决方案1】:

您可以只使用隐式样式和触发器:

<DataGrid.Resources>
    <Style TargetType="DataGridCell">
        <Style.Triggers>
            <DataTrigger Binding="{Binding MyBoolean}" Value="True">
                <Setter Property="TextElement.FontWeight" Value="Bold" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

(如果您有更多列,您可以只使用列上的样式 (ElementStyle & ElementEditingStyle) 来限制效果)

【讨论】:

  • 我从未使用过这些。我会调查的。感谢您的快速回复。
  • 我终于设法使用您的解决方案做到了。谢谢!
  • 现在我必须在 Silverlight 和 Style.Triggers 中执行此操作!什么是替代方案?我可能会为此提出一个新问题。
  • @DEIONaLiMs:您可以将 FontWeight 直接绑定到 bool 并在绑定中应用 Converter
  • 我已经这样做了。不过,不是使用 bool 和 Converter,而是使用 Direct FontWeight,就​​像我在问题中解释的那样。我是否必须通过转换器才能使其工作?
【解决方案2】:

在这种情况下,我通常会创建一个专门用于绑定的“模型对象”。因此,不是绑定到“Customer”的可观察集合,而是绑定到“CustomerModel”的可观察集合,其中模型对象具有“CustomerName”属性,然后是与所需字体相对应的一些其他属性(实际字体对象,或者通过值转换器解析的某种枚举,如果你不希望你的 VM 层知道视图问题)。该模型对象可以根据您提到的布尔属性确定可用的内容。

【讨论】:

  • 我试过这个方法,但字体从未设置为粗体。我以这种方式绑定 FontWeight: FontWeight="{Binding MyFont}" 其中 MyFont 是一个 FontWeight 设置为 FontWeights.Normal 或 FontWeights.Bold,具体取决于构造函数中的布尔值。我想提出一些 PropertyCahngedEvent 吗?
  • 是的,每当您更改绑定对象中的字体粗细时,您都希望引发属性更改事件以通知 GUI。
  • 它不起作用。我不知道我做错了什么。当我放置一个断点时,我可以看到网格在通过它的 getter 时请求了“CustomerName”属性。但它不会通过 MyFont getter。
  • 也许更新您的问题,以便我们可以查看您的 XAML 和背后的相关代码...
  • 我一下班就会这样做。感谢您的帮助。
【解决方案3】:

这是我使用 H.B 解决方案成功实现的方法:

            <DataGrid.Resources>
            <Style x:Key="Style1" TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsRead}" Value="False">
                        <Setter Property="FontWeight" Value="Bold" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Title}" ElementStyle="{StaticResource ResourceKey=Style1}" />
        </DataGrid.Columns>

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2011-07-29
    相关资源
    最近更新 更多