如何根据在Silverlight中使用MVVM绑定的数据来更改DataGrid的单元格? [英] How can I change a cell of a DataGrid depending on the data it is bound to using MVVM in Silverlight?
问题描述
这是这种情况:我正在使用MVVM Light Toolkit,并且我有一个DataGrid绑定到一个ObservableCollection。只显示一个文本列。我希望单元格的文本为粗体或正常,具体取决于显示对象内的布尔值。
为了使其在WCF中工作,我使用了一个Syle.Trigger的风格:
< DataGrid.Resources>
< Style x:Key =Style1TargetType ={x:Type TextBlock}>
< Style.Triggers>
< DataTrigger Binding ={Binding IsRead}Value =False>
< Setter Property =FontWeightValue =Bold/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGrid.Resources>
< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding Title}ElementStyle ={StaticResource ResourceKey = Style1}/>
< /DataGrid.Columns>
问题是这在Silverlight中不可用。我做了一些研究,发现VisualStateManager是触发器的替换,但我从未设法创建一个编辑DataGridTextColumn的状态。我尝试了从GoToState到DataStateBehavior的一些不同的方式...没有任何工作到目前为止。
请帮助!
Thx
FontWeight
属性对于 DataGridTextColumn
列类型不能正常工作。因此,您应该将此列更改为 DataGridTemplateColumn
类型,其中包含 TextBlock
控件,并将表达式混合数据触发器应用于其中
以下是代码:
< 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>粗体< / FontWeight>
< / ic:ChangePropertyAction.Value>
< / ic:ChangePropertyAction>
< / ic:DataTrigger>
< / i:Interaction.Triggers>
< / TextBlock>
< / DataTemplate>
< / sdk:DataGridTemplateColumn.CellTemplate>
< / sdk:DataGridTemplateColumn>
命名空间 i
和 ic
的定义如下:
xmlns:i =clr-namespace:System.Windows .Interactivity; assembly = System.Windows.Interactivity
xmlns:ic =clr-namespace:Microsoft.Expression.Interactivity.Core; assembly = Microsoft.Expression.Interactions
安装Expression Blend SDK时,必需的库可用。
可以使用SDK,您可以使用替代方法并写入一个值转换器:
< TextBlock Text ={Binding Title }VerticalAlignment =Center
FontWeight ={Binding IsRead,Converter = {StaticResource BooleanToFontWeightConverter}}/>
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
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}}" />
这篇关于如何根据在Silverlight中使用MVVM绑定的数据来更改DataGrid的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!