How can I change a cell of a DataGrid depending on the data it is bound to using MVVM in Silverlight?

那年仲夏 提交于 2019-12-11 04:09:35

问题


I recently posted the same question but for WPF and got an answer. The thing is that answer doesn't work in Silverlight. Here is the situation: I'm using the MVVM Light Toolkit and I have a DataGrid bound to an ObservableCollection. There is only one text column displayed. I'd like the text of the cell to be Bold or Normal depending on a boolean that is inside the object displayed. To make it work in WCF i used a Style with a Syle.Trigger:

<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> 

The problem is that this is not available in Silverlight. I made some research and found that VisualStateManager is the "replacement" for triggers but I never managed to create a state that edits the DataGridTextColumn. I tried a bunch of different ways from GoToState to DataStateBehavior... nothing worked so far.

Please Help !

Thx


回答1:


The FontWeight property doesn't work well with the DataGridTextColumn column type. So you should change this column to the DataGridTemplateColumn type with the TextBlock control inside and apply the expression blend data trigger to it.

Here is the code:

<sdk:DataGridTemplateColumn>
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" VerticalAlignment="Center">
                <i:Interaction.Triggers>
                    <ic:DataTrigger Binding="{Binding IsRead}" Value="False">
                        <ic:ChangePropertyAction PropertyName="FontWeight" >
                            <ic:ChangePropertyAction.Value>
                                <FontWeight>Bold</FontWeight>
                            </ic:ChangePropertyAction.Value>
                        </ic:ChangePropertyAction>
                    </ic:DataTrigger>
                </i:Interaction.Triggers>
            </TextBlock>
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

The namespaces i and ic are defined so:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions"

The necessary libraries become available when you install Expression Blend SDK.

If you don't have the SDK, you can use the alternative approach and write a value converter:

<TextBlock Text="{Binding Title}" VerticalAlignment="Center" 
    FontWeight="{Binding IsRead, Converter={StaticResource BooleanToFontWeightConverter}}" />


来源:https://stackoverflow.com/questions/8286666/how-can-i-change-a-cell-of-a-datagrid-depending-on-the-data-it-is-bound-to-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!