Why databinding TwoWay don't work on the text property of a combobox in .net 4.0 (it's working in .net 3.5)?
My code:
I have an xml file like this:
<xml>
  <combobox option="" obs="tralala">
    <option value="here" />
    <option value="there" />
  </combobox>
  <combobox option="blue" obs="">
    <option value="one" />
    <option value="two" />
    <option value="three" />
  </combobox>
</xml>
and I have a ListItem control like that:
<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
         ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
         IsSynchronizedWithCurrentItem="True">
   <ListBox.ItemTemplate>
    <DataTemplate>
      <DockPanel LastChildFill="True">
        <ComboBox MinWidth="75" IsEditable="True"
                  IsReadOnly="False" DockPanel.Dock="Left"
                  DataContext="{Binding Path=Element[combobox ]}"
                  IsSynchronizedWithCurrentItem="False"
                  ItemsSource="{Binding Path=Elements[option], UpdateSourceTrigger=PropertyChanged}"
                  DisplayMemberPath="Attribute[value].Value"
                  Text="{Binding Path=Attribute[option].Value, UpdateSourceTrigger=PropertyChanged}"
                  />
        <TextBox MinWidth="150" AcceptsReturn="False"
                 AcceptsTab="False" TextWrapping="NoWrap"
                 Text="{Binding Path=Attribute[obs].Value, UpdateSourceTrigger=PropertyChanged}" />
      </DockPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
Here is the code behind:
XDocument xdXml;
public MyWindow()
{
    xdXml = XDocument.Load(@"C:\file.xml");
    InitializeComponent();
    DataContext = xdXml;
    xdXml.Changed += new EventHandler<XObjectChangeEventArgs>(XdXml_Changed);
}
private void XdXml_Changed(object sender, XObjectChangeEventArgs e)
{
    xdXml.Save(@"C:\fichier.xml");
}
I do like that because I can have a ComboBox with auto-completion with the different custom option for each, but I can write what I want, and the result is in the attribute option of the element <combobox>
It work fine if I target .net 3.5, but only textbox bind if I target .net 4.0
Why? What can I do?
Here is the solution for doing this code working with framework 4.0 (I've tried to adapt it to your exemple, but i'm not sure. Anyway, this is the idea):
Change your ListItem control like that:
<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
         ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
         IsSynchronizedWithCurrentItem="True">
   <ListBox.ItemTemplate>
    <DataTemplate>
      <DockPanel LastChildFill="True">
        <!-- Add this collapsed textbox -->
        <TextBox Visibility="Collapsed" DataContext="{Binding Path=Element[combobox]}" Text="{Binding Path=Text, ElementName=cbxComboBox, UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBox_TextChanged" />
        <!-- Name the Combobox -->
        <ComboBox Name="cbxComboBox" MinWidth="75" IsEditable="True"
                  IsReadOnly="False" DockPanel.Dock="Left"
                  DataContext="{Binding Path=Element[combobox]}"
                  IsSynchronizedWithCurrentItem="False"
                  ItemsSource="{Binding Path=Elements[option], UpdateSourceTrigger=PropertyChanged}"
                  DisplayMemberPath="Attribute[value].Value"
                  Text="{Binding Path=Attribute[option].Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                  />
        <TextBox MinWidth="150" AcceptsReturn="False"
                 AcceptsTab="False" TextWrapping="NoWrap"
                 Text="{Binding Path=Attribute[obs].Value, UpdateSourceTrigger=PropertyChanged}" />
      </DockPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
And your new code behind is:
XDocument xdXml;
public MyWindow()
{
    xdXml = XDocument.Load(@"C:\file.xml");
    InitializeComponent();
    DataContext = xdXml;
    xdXml.Changed += new EventHandler<XObjectChangeEventArgs>(XdXml_Changed);
}
private void XdXml_Changed(object sender, XObjectChangeEventArgs e)
{
    xdXml.Save(@"C:\fichier.xml");
}
// finally, add this event:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (((XElement)((FrameworkElement)sender).DataContext).Attribute("option").Value != ((TextBox)sender).Text)
    {
        ((XElement)((FrameworkElement)sender).DataContext).Attribute("option").Value = ((TextBox)sender).Text;
    }
}
For understand, take a look at:
For the moment, the only real solution i found to this problem is to not target Framework 4.0 but Framework 3.5...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With