locked
dynamic two-way binding using Linq to XML in DataGrid in WPF application RRS feed

  • Question

  • Can someone give me an example of how to use dynamic two-way binding using Linq to XML where the source is an XML file loaded into an XElement and the target is a grid cell.  I have found many articles and posts that imply it can be done, but noone has provided an example that I can make work.

    I'm using Visual Studio 2010, C#, and the application is WPF.  I've tried many many many things, but the most recent attempt is as follows:

    In XAML I have a datagrid control.  The ItemSource property is set to "{Binding}".  The columns are composed of DataGridTemplateColumn elements.  These elements contain TexBoxes.  The TextBoxes Text attribute is set to "{Binding Path=Attribute[value].Value}".

      <DataGrid x:Name="substitutionDataGrid" Grid.Row="2"
         AutoGenerateColumns="False"
         HorizontalAlignment="Stretch"
         Margin="20,20,20,20"
         VerticalAlignment="Stretch"
         Style="{StaticResource DataGridStyle}"
         ItemsSource="{Binding}">
       <DataGrid.Columns>
        <!--<DataGridTextColumn x:Name="firstColumn" Header="First" Binding="{Binding Path=Attribute[key].Value,Mode=TwoWay}"/>-->
        <DataGridTemplateColumn Header="Key">
         <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
           <TextBox x:Name="keyCellTextbox" Text="{Binding Path=Attribute[key].Value, Mode=TwoWay}"/>
          </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Value">
         <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
           <TextBox x:Name="valueCellTextbox" Text="{Binding Path=Attribute[value].Value}" />
          </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
       </DataGrid.Columns>
      </DataGrid>
    
    

    In the code behind I have an XElement object into which I load my XML file.  I set the DataContext attribute of the DataGrid control to be equal to the set of elements under the root in the XML file.

      XElement _subData;
      string _xmlFile = @"C:\Temp\SubstitutionStrings.xml";
    
      public MainWindow()
      {
       InitializeComponent();
       _subData = XElement.Load(_xmlFile);
       substitutionDataGrid.DataContext = _subData.Elements("Pair");
       
      }
    
      private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
       if (true) // replace with IsDirty
       {
        MessageBoxResult result = MessageBox.Show("Save values?", "Closing", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Cancel);
        switch (result)
        {
         case MessageBoxResult.Cancel:
          e.Cancel = true;
          break;
         case MessageBoxResult.No:
          break;
         case MessageBoxResult.Yes:
          _subData.Save(_xmlFile);
          break;
         default:
          break;
        }
       }
      }
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <Root>
     <Pair key="thing1" value="thing2" />
     <Pair key="thing3" value="thing4" />
     <Pair key="thing5" value="thing6" />
    </Root>
    

    This all works in the sense that the data appears in my DataGrid and I can edit it.  However, in my Window_Closing event handler I want to save the changes, but I find that the changes I made in the DataGrid were not transferred back to the bound XElement object.

    Any examples or pointers are welcolme.

    Thanks,
    Dogulas

    • Moved by Helen Zhou Friday, February 4, 2011 7:18 AM (From:Windows Forms Data Controls and Databinding)
    Thursday, February 3, 2011 8:31 PM

Answers

  • Hello Dogulas,

    This forum is dedicated to Windows Forms technology. I'm moving this thread from Base "Windows Forms" to "Windows Presentation Foundation" forum, since this issue is related to WPF. There are more WPF experts in that forum.

    Best Regards,

     


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Dogulas Friday, February 4, 2011 10:48 PM
    Friday, February 4, 2011 7:18 AM
  • Hi Dogulas,

    Please specify the Binding.UpdateSourceTrigger to PropertyChanged as below:

     <DataGrid.Columns>
       <!--<DataGridTextColumn x:Name="firstColumn" Header="First" Binding="{Binding Path=Attribute[key].Value,Mode=TwoWay}"/>-->
       <DataGridTemplateColumn Header="Key">
         <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
             <TextBox x:Name="keyCellTextbox" Text="{Binding Path=Attribute[key].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
           </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>
       <DataGridTemplateColumn Header="Value">
         <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
             <TextBox x:Name="valueCellTextbox" Text="{Binding Path=Attribute[value].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
           </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>
     </DataGrid.Columns>
    

    @Any moderator in WPF-RC forum, please help to move it to WPF forum: http://social.msdn.microsoft.com/Forums/en-US/wpf/threads

    Thanks.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Dogulas Friday, February 4, 2011 10:48 PM
    Friday, February 4, 2011 8:25 AM

All replies

  • Hello Dogulas,

    This forum is dedicated to Windows Forms technology. I'm moving this thread from Base "Windows Forms" to "Windows Presentation Foundation" forum, since this issue is related to WPF. There are more WPF experts in that forum.

    Best Regards,

     


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Dogulas Friday, February 4, 2011 10:48 PM
    Friday, February 4, 2011 7:18 AM
  • Hi Dogulas,

    Please specify the Binding.UpdateSourceTrigger to PropertyChanged as below:

     <DataGrid.Columns>
       <!--<DataGridTextColumn x:Name="firstColumn" Header="First" Binding="{Binding Path=Attribute[key].Value,Mode=TwoWay}"/>-->
       <DataGridTemplateColumn Header="Key">
         <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
             <TextBox x:Name="keyCellTextbox" Text="{Binding Path=Attribute[key].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
           </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>
       <DataGridTemplateColumn Header="Value">
         <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
             <TextBox x:Name="valueCellTextbox" Text="{Binding Path=Attribute[value].Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
           </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>
     </DataGrid.Columns>
    

    @Any moderator in WPF-RC forum, please help to move it to WPF forum: http://social.msdn.microsoft.com/Forums/en-US/wpf/threads

    Thanks.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Dogulas Friday, February 4, 2011 10:48 PM
    Friday, February 4, 2011 8:25 AM
  • Helen Zhou,

    I appologize for the bad location.  When I asked in which forum I should post this in this post:

    http://social.msdn.microsoft.com/Forums/en-US/whatforum/thread/0d77cc88-a63e-426e-8c64-ae100b383158

    I was sent here.  When I questioned this choice, I got no reply.  Thank you for moving it for me.

    Thanks,
    Dogulas

    Friday, February 4, 2011 10:40 PM
  • Bob Bao,

    Thanks for your response.  That worked perfectly.  I had searched for this for several days.  Now that I know what to look for, I can easily find it.

    Thanks,
    Dogulas

    Friday, February 4, 2011 10:47 PM