2015-06-17 35 views
5

Mam wartość datagrid niż zawiera wartość pochodząca z stored procedure. Wszystkie wartości są ustawione na Bold jako FontWeight.WPF DataGrid Trigger na zawartość komórki

chciałbym aby tekst normalny, gdy zawartość komórki jest równa 0.

Jak mogę zrobić z wyzwalaczem?

to zrobić, ale wydaje się nie działa

<DataGrid.CellStyle> 
    <Style TargetType="DataGridCell"> 
      <Setter Property="FontWeight" Value="Bold" /> 
      <Style.Triggers> 
       <Trigger Property="Content" Value="0"> 
        <Setter Property="FontWeight" Value="Normal"/> 
       </Trigger> 
      </Style.Triggers> 
    </Style> 
</DataGrid.CellStyle> 

Odpowiedz

7

Nie można uzyskać dostępu DataGridCell.Content ten sposób, zamiast używać DataTrigger na podstawie swojej DataGrid.SelectedItem.YourProperty tak:

<DataGrid.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="FontWeight" Value="Bold" /> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding YourProperty}" Value="0"> 
        <Setter Property="FontWeight" Value="Normal"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </DataGrid.CellStyle> 

EDIT:

Zakładając, że DataGridColumns są tekstowy następnie można użyć IValueConverter jak poniżej:

Należy pamiętać, że jeśli niektóre kolumny siatki danych nie są oparte na tekście , to rozwiązanie nadal działa dla tych kolumn, które są.

Xaml:

<Window.Resources> 
    <local:FontWeightConverter x:Key="fontWeightConverter"/> 
</Window.Resources> 

...

<DataGrid.CellStyle> 
     <Style TargetType="{x:Type DataGridCell}"> 
      <Style.Setters> 
       <Setter Property="FontWeight" 
         Value="{Binding RelativeSource={RelativeSource Self}, 
         Path=Content.Text, 
         Converter={StaticResource fontWeightConverter}}" /> 
      </Style.Setters> 
     </Style> 
    </DataGrid.CellStyle> 

Przelicznik:

public class FontWeightConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, 
     object parameter, CultureInfo culture) 
    { 
     if (value != null && value.ToString() == "0") 
      return FontWeights.Normal; 
     return FontWeights.Bold; 
    } 

    public object ConvertBack(object value, Type targetType, 
     object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

Brzmi dobrze, ale potrzebuję go dla wszystkich właściwości mojego obiektu. Nie chcę replikować 12 razy tego kodu, który jestem pewny, że pracuję – Galma88

+0

Perfect. Twoja edycja działa. Zakładam więc, że nie ma możliwości robienia tego tylko w XAML. Czy to jest poprawne? – Galma88

+1

Jestem w 99% pewien, że nie ma;) –

2

Jest to sposób, aby określić tę kolumnę:

<DataGridTemplateColumn> 
     <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
          <TextBox Text="{Binding DataBaseValue}"/> 
        </StackPanel> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

można dodać wiążące FontWeightof z TextBox z konwerterem skojarzona z Tekst sam w sobie.

+0

Mam 12 kolumn. Nie chcę replikować tego kodu dla wszystkich kolumn. – Galma88

1

Można to zrobić -

<DataGrid.CellStyle> 
    <Style TargetType="DataGridCell"> 
      <Setter Property="FontWeight" Value="Bold" /> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Path=Content.Text, Mode=OneWay, RelativeSource={RelativeSource Self}}" Value="0"> 
        <Setter Property="FontWeight" Value="Normal"/> 
       </Trigger> 
      </Style.Triggers> 
    </Style> 
</DataGrid.CellStyle>