我有一个datagrid,它有几个静态和更动态生成的coloumns。使用多值转换器成功调整动态添加的coloumns单元的背景图像。现在我需要向这些单元格添加内容,而不仅仅是改变它们的背景。我希望使用多值转换器来实现这种目的,但它没有。 在调试器中运行MultiValueConverter,它返回一个字符串,但单元格保持为空。
请参阅代码,其中背景设置正在运行但内容设置没有。除了gridCellBgConverter返回Brush之外,它们必须是相同的代码,gridCellContentConverter返回字符串。
<DataGrid DockPanel.Dock="Top" Name="main_dg" HorizontalAlignment="Stretch" HorizontalScrollBarVisibility="Hidden" Margin="3"
AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="Cell"
SelectedCellsChanged="main_dg_SelectedCellsChanged" CanUserReorderColumns="False"
CanUserAddRows="False" CellEditEnding="main_dg_CellEditEnding" KeyUp="main_dg_KeyUp"
FontSize="14" FontWeight="Normal" FontStretch="Normal" SnapsToDevicePixels="True" TextOptions.TextFormattingMode="Display" RenderOptions.EdgeMode="Aliased">
<DataGrid.Resources>
<Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
<Setter Property="Padding" Value="-2"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource gridCellBgConverter}" UpdateSourceTrigger="PropertyChanged">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"></Binding>
<Binding Path="." UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.SelectedStationIndex" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.TrainOnTrackElementId" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.LastStationCountDownChanged" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.SelectedStationDelay" UpdateSourceTrigger="PropertyChanged"></Binding>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Content">
<Setter.Value>
<MultiBinding Converter="{StaticResource gridCellContentConverter}" UpdateSourceTrigger="PropertyChanged">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"></Binding>
<Binding Path="." UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.SelectedStationIndex" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.TrainOnTrackElementId" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.LastStationCountDownChanged" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.SelectedStationDelay" UpdateSourceTrigger="PropertyChanged"></Binding>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
WPF的固有属性是否无法通过Style和ValueConverter设置内容? 你知道如何解决这个问题吗?
提前致谢: Ferenc的
解决方案
在AngelWPF的帮助下,我能够解决我的问题:
动态创建的coloumns类型是DataGridTemplateColoumn
样式设定器是:
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource gridCellContentConverter}" UpdateSourceTrigger="PropertyChanged">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"></Binding>
<Binding Path="." UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.SelectedStationIndex" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.TrainOnTrackElementId" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.LastStationCountDownChanged" UpdateSourceTrigger="PropertyChanged"></Binding>
<Binding Path="TrainToAnnounceViewModel.SelectedStationDelay" UpdateSourceTrigger="PropertyChanged"></Binding>
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</Setter.Value>
并在MultiValueConverter而不是
public object Convert(object[] values,
Type targetType, object parameter,
CultureInfo culture) {
var cell = (DataGridCell)values[0];
我必须使用
public object Convert(object[] values,
Type targetType, object parameter,
CultureInfo culture) {
var textBlock = (TextBlock)values[0];
var cell = (DataGridCell)((ContentPresenter)textBlock.TemplatedParent).TemplatedParent
访问原始单元格。 我毫不怀疑有更好的方法可以做到这一点,但似乎有效:)
答案 0 :(得分:0)
假设您的数据网格是只读的(不可编辑),可以按照以下方式实现。
(以下代码未经过测试)
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="0"
SnapsToDevicePixels="True">
<TextBlock>
<TextBlock.Text>
<!-- Your MultiBinding Here -->
</TextBlock.Text>
</TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
关于这种方法的注意事项是你在这方面失去了许多单元级功能,例如验证,编辑模式,背景颜色,鼠标悬停,选择颜色等......
我仍然建议使用DataGridTemplate列和CellTemplate属性。