2011-08-04 47 views
1

我有一個datagrid有幾個靜態和更動態生成coloumns。動態添加的coloumns單元的背景圖像可以用多值轉換器成功調整。現在我需要添加內容到這些單元格不僅改變他們的背景。我希望使用一個多值轉換器來處理這個垃圾,但是它不會。 在MultiValueConverter運行的調試器中,它返回一個字符串,但單元格保持空白。WPF:可以通過MultiValueConverter設置DataGridCell的內容嗎?

請參閱代碼,其中後臺設置正在工作,但內容設置沒有。除了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的固有屬性,內容無法通過風格和ValueConverter設置? 你有想法如何嘗試解決這個問題嗎?

在此先感謝: 費倫茨解決方案

類具AngelWPF的幫助,我能解決我的proplem:

  1. 動態創建coloumns類型是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 

訪問原來的小區。 我毫不懷疑,也有更好的方法來做到這一點普蘭蒂,但似乎工作:)

回答

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

+0

幫我解決我的問題。謝謝。 – fec77

相關問題