2016-01-26 56 views
0

我希望我可以清楚地解釋它....
我們將DataGrid綁定到來自某個數據源的集合。 每個列的屬性都在不同的集合中描述,所以我們在運行時創建列並根據屬性集合中的值在列上設置屬性(例如,只讀)。以編程方式將轉換器綁定到DataGrid中的列

新要求是「必需」屬性。對於需要的列,我想綁定一個轉換器,該轉換器根據該值設置DataGridCell的背景顏色。 (如果單元格是空的,轉換器最簡單的情況會是一些顏色,如果用戶輸入了一個值,則是白色。我相信未來會有更復雜的驗證。)

我認爲它可以完成在像什麼,我現在用修補:

<Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Border BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         Background="{TemplateBinding Background}" 
         SnapsToDevicePixels="True"> 
        <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"> 
        </TextBox> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

(仍需要在一些地方加入轉爐....)
或將是我想做的事情在後臺代碼做?任何指針將不勝感激...

回答

0

這是一種做法。 IDK,如果它是最好的方式,但它的工作原理,它已經過了幾個小時,因爲你問這麼... ...

你的DataGridCell充滿了邊框/文本框,所以我假設你想改變文本框的背景顏色因爲你不會看到DataGridCell的背景。

既然你提到可能有更復雜的場景在未來,我用了一個multibinding與轉換器和(通過使用綁定</>)和它的文本值在文本框的datacontext傳遞。

<Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type DataGridCell}"> 
       <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
        <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"> 
         <TextBox.Resources> 
          <local:ValidationBGConverter x:Key="ValidationBGConverter" /> 
         </TextBox.Resources> 
         <TextBox.Style> 
          <Style TargetType="TextBox"> 
           <Setter Property="Background"> 
            <Setter.Value> 
             <MultiBinding Converter="{StaticResource ValidationBGConverter}"> 
              <Binding /> 
              <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content.Text" /> 
             </MultiBinding> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </TextBox.Style> 
        </TextBox> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

這裏是轉換器:

public class ValidationBGConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (values.Length != 2) 
      return Brushes.Black; 
     var datacontext = values[0] as ViewData; // Or whatever the textbox's datacontext object is 
     if (datacontext != null) // If null, probably the new item row 
     { 
      var txt = values[1] as string; // Textbox text 
      if (string.IsNullOrWhiteSpace(txt)) 
       return Brushes.Red; 
      if (txt.Length < 3) 
       return Brushes.Pink; 
      if (txt.Length > 5) 
       return new LinearGradientBrush(Colors.White, Colors.Blue, 90.0); 
     } 
     return Brushes.White; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

而且,截圖:

enter image description here

+0

非常有幫助,謝謝。特別是關於傳遞DataContext作爲參數的部分:。 – Number8

+0

不客氣。你能接受答案嗎?謝謝。 –

相關問題