Valores de referencia de disparador múltiple de cuadrícula de datos del código subyacente

I'm still bloody green in WPF and have not yet fully grasped the concept behind it. I've got the following problem:

I want to set triggers in a datagrid depending on a precondition.

Ejemplo:

In my code-behind, I have a string variable, let's call it variableString. Now depending on the the value of variableString, I want to enable/disable the triggers inside a datagrid, that I have defined in XAML like:

if(variableString == "a") 

luego

XAML

<DataGrid AutoGenerateColumns="False" Margin="5,5,0,75" Name="dataGrid1" ItemsSource="Binding}">
  <DataGrid.ItemContainerStyle>
        <Style TargetType="DataGridRow">
             <Style.Triggers>
                  <DataTrigger Binding="{Binding Path=SomeColumnName}" Value="someValue">
                  <Setter Property="Background" Value="White"/>
                  <DataTrigger Binding="{Binding Path=SomeColumName}" Value="someOtherValue">
                  <Setter Property="Background" Value="Red"/>
             </Style.Triggers>
        </Style>
   </DataGrid.ItemContainerStyle>

De lo contrario, si

if(variableString == "b")

luego

Do Nothing`

I've already tried binding the string to the datacontext of the datagrid, but that was rather contra-productive, as it removes my binding to the database.

Can anyone help me here. An example, a push in the right direction etc...

I really like the options that WPF gives you, however it's that fundamental things, that were so easy to handle in WinForms, that drive me mad in WPF.

Muchas Gracias

preguntado el 03 de mayo de 12 a las 14:05

1 Respuestas

Creo que quieres un MultiDataTrigger, which allows you to base your trigger off of multiple values

  <MultiDataTrigger>
    <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding Path=SomeColumnName}" Value="someValue" />
      <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=variableString}" Value="A" />
    </MultiDataTrigger.Conditions>
    <Setter Property="Background" Value="White" />
  </MultiDataTrigger>

To find your string in the code behind, you'll probably have to use some kind of RelativeSource binding to find the class containing that property. My example assumes there is a public property called variableString al Window clase

contestado el 03 de mayo de 12 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.