1

我有一个数据网格,它有几个静态和更多动态生成的列。使用多值转换器成功调整了动态添加的列单元格的背景图像。现在我需要向这些单元格添加内容,不仅要更改它们的背景。我希望为此目的使用多值转换器也能正常工作,但事实并非如此。在调试器中 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设置Content吗?你知道如何尝试解决这个问题吗?

提前致谢:费伦茨

解决方案

在 AngelWPF 的帮助下,我能够解决我的问题:

  1. 动态创建的列类型是 DataGridTemplateColoumn

  2. 样式设置器是:

    <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

访问原始单元格。我毫不怀疑有更好的方法可以做到这一点,但它似乎有效:)

4

1 回答 1

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 属性。

于 2011-08-04T14:08:13.357 回答