Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF DataGridCell Binding Problem

WPF DataGridCell Binding Problem

Scheduled Pinned Locked Moved WPF
wpfhelpcsharpcsswcf
5 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have a DataGrid bound to a list of models. On the ViewModel is a property called AreFieldsEnabled. When the user clicks Edit the it sets AreFieldsEnabled to true and the datagrid becomes enabled/ I have this style

    <Style x:Key="dataGridCurrencyCellStyle"
    BasedOn="{StaticResource dataGridCellStyle}"
    TargetType="{x:Type DataGridCell}">

    <Setter Property="Width" Value="75"/>
    <Setter Property="TextBlock.TextAlignment" Value="Left"/>
    <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/>
    <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
    

    </Style>

    What I need do now is a change it to a MultiTrigger. On the model is a bool property called IsCellEnabled which I set programmatically when loading the data.

    <Style x:Key="dataGridCurrencyCellStyle"
    BasedOn="{StaticResource dataGridCellStyle}"
    TargetType="{x:Type DataGridCell}">

    <Setter Property="Width" Value="75"/>
    <Setter Property="TextBlock.TextAlignment" Value="Left"/>
    <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/>
    <Setter Property="IsEnabled" Value="False"/>
    
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="AreFieldsEnabled" Value="True" />
                <Condition Property="IsCellEnabled " Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="IsEditing" Value="True" />
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
    

    </Style>

    Now, both conditions are wrong and won't compile. So, the AreFieldsEnabled property is on the ViewModel. The IsCellEnabled property is on the Model bound to the grid. What's the right way to do this?

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I have a DataGrid bound to a list of models. On the ViewModel is a property called AreFieldsEnabled. When the user clicks Edit the it sets AreFieldsEnabled to true and the datagrid becomes enabled/ I have this style

      <Style x:Key="dataGridCurrencyCellStyle"
      BasedOn="{StaticResource dataGridCellStyle}"
      TargetType="{x:Type DataGridCell}">

      <Setter Property="Width" Value="75"/>
      <Setter Property="TextBlock.TextAlignment" Value="Left"/>
      <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/>
      <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
      

      </Style>

      What I need do now is a change it to a MultiTrigger. On the model is a bool property called IsCellEnabled which I set programmatically when loading the data.

      <Style x:Key="dataGridCurrencyCellStyle"
      BasedOn="{StaticResource dataGridCellStyle}"
      TargetType="{x:Type DataGridCell}">

      <Setter Property="Width" Value="75"/>
      <Setter Property="TextBlock.TextAlignment" Value="Left"/>
      <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/>
      <Setter Property="IsEnabled" Value="False"/>
      
      <Style.Triggers>
          <MultiTrigger>
              <MultiTrigger.Conditions>
                  <Condition Property="AreFieldsEnabled" Value="True" />
                  <Condition Property="IsCellEnabled " Value="True" />
              </MultiTrigger.Conditions>
              <MultiTrigger.Setters>
                  <Setter Property="IsEditing" Value="True" />
              </MultiTrigger.Setters>
          </MultiTrigger>
      </Style.Triggers>
      

      </Style>

      Now, both conditions are wrong and won't compile. So, the AreFieldsEnabled property is on the ViewModel. The IsCellEnabled property is on the Model bound to the grid. What's the right way to do this?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Use a MultiDataTrigger when the conditions depend on a binding rather than the styled control's properties.

      <Style.Triggers>
      <MultiDataTrigger>
      <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" />
      <Condition Binding="{Binding IsCellEnabled}" Value="True" />
      </MultiDataTrigger.Conditions>
      <MultiDataTrigger.Setters>
      <Setter Property="IsEditing" Value="True" />
      </MultiDataTrigger.Setters>
      </MultiDataTrigger>
      </Style>

      MultiDataTrigger Class (System.Windows) | Microsoft Docs[^] WPF MultiTrigger and MultiDataTrigger - The complete WPF tutorial[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        Use a MultiDataTrigger when the conditions depend on a binding rather than the styled control's properties.

        <Style.Triggers>
        <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" />
        <Condition Binding="{Binding IsCellEnabled}" Value="True" />
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
        <Setter Property="IsEditing" Value="True" />
        </MultiDataTrigger.Setters>
        </MultiDataTrigger>
        </Style>

        MultiDataTrigger Class (System.Windows) | Microsoft Docs[^] WPF MultiTrigger and MultiDataTrigger - The complete WPF tutorial[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Thanks. That only partly fixed it

        <Style x:Key="dataGridCurrencyCellStyle"
        BasedOn="{StaticResource dataGridCellStyle}"
        TargetType="{x:Type DataGridCell}">

        <Setter Property="Width" Value="75"/>
        <Setter Property="TextBlock.TextAlignment" Value="Left"/>
        <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/>
        <Setter Property="IsEnabled" Value="False"/>
        
        <Style.Triggers>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" />
                    <Condition Binding="{Binding IsCellEnabled}" Value="True" />
                </MultiDataTrigger.Conditions>
                <MultiDataTrigger.Setters>
                    <Setter Property="IsEnabled" Value="True" />
                </MultiDataTrigger.Setters>
            </MultiDataTrigger>
        </Style.Triggers>
        

        </Style>

        The 2nd condition with the IsCellEnabled isn't finding the model the row is bound to. Not sure how to reference that from this style. I get a binding error "BindingExpression path error: 'IsCellEnabled' property not found on 'object' ''DataRowView'"

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Use a MultiDataTrigger when the conditions depend on a binding rather than the styled control's properties.

          <Style.Triggers>
          <MultiDataTrigger>
          <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" />
          <Condition Binding="{Binding IsCellEnabled}" Value="True" />
          </MultiDataTrigger.Conditions>
          <MultiDataTrigger.Setters>
          <Setter Property="IsEditing" Value="True" />
          </MultiDataTrigger.Setters>
          </MultiDataTrigger>
          </Style>

          MultiDataTrigger Class (System.Windows) | Microsoft Docs[^] WPF MultiTrigger and MultiDataTrigger - The complete WPF tutorial[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #4

          I'm still stuck on this. I can't seem to get the second trigger condition to bind correctly. How would I bind the condition to the IsCellEnabled property on the model?

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          Richard DeemingR 1 Reply Last reply
          0
          • K Kevin Marois

            I'm still stuck on this. I can't seem to get the second trigger condition to bind correctly. How would I bind the condition to the IsCellEnabled property on the model?

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Kevin Marois wrote:

            'IsCellEnabled' property not found on 'object' ''DataRowView'"

            You're not binding to the model; you're binding to a DataView. You could try {Binding [IsCellEnabled]}, assuming that is a column in your view.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups