简体   繁体   English

XAML - 为什么数据绑定TwoWay不能在.net 4.0中的组合框的text属性上工作?

[英]XAML - Why databinding TwoWay don't work on the text property of a combobox in .net 4.0?

Why databinding TwoWay don't work on the text property of a combobox in .net 4.0 (it's working in .net 3.5)? 为什么数据绑定TwoWay不能在.net 4.0中的组合框的文本属性上工作(它在.net 3.5中工作)?

My code: 我的代码:

I have an xml file like this: 我有一个像这样的xml文件:

<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: 我有一个像ListItem控件:

<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> 我喜欢这样,因为我可以使用自动完成的ComboBox ,每个都有不同的自定义选项,但我可以写出我想要的内容,结果是在元素<combobox>的属性选项中

It work fine if I target .net 3.5, but only textbox bind if I target .net 4.0 如果我的目标是.net 3.5,它可以正常工作,但如果我的目标是.net 4.0,则只能绑定文本框

Why? 为什么? What can I do? 我能做什么?

目前,我发现这个问题的唯一真正解决方案是不针对Framework 4.0而是针对Framework 3.5 ......

Coucou, j'ai trouvé une solution pour notre problème (en tout cas, ça marche pour moi) ! Coucou,j'aitrouvéunesolution pournotreproblème(en tout cas,çamarchepour moi)! Dit moi si ça marche pour toi aussi, mais y'a pas de raison ^^. Dit moisiçamarchepour toi aussi,mais y'a pas de raison ^^。

Par contre, je ne suis pas sure d'avoir bien compris pourquoi un tel changement entre le framework 3.5 dans lequel ça marchait bien, et le 4.0... Par contre,je ne suis pas sure d'avoir bien包括pourquoi un tel changement entre le framework 3.5 danslequelçamarchaitbien,et le 4.0 ...

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): 这是使用框架4.0进行此代码的解决方案(我已经尝试将其调整为您的例子,但我不确定。无论如何,这是个想法):

Change your ListItem control like that: 像这样更改ListItem控件:

<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: 要了解,请查看:

J'espère vraiment que ça marchera pour toi aussi, n'hésite pas à me demander si besoins ! J'espèrevraimentqueçamarcherapour toi aussi,n'hésitepasàmedemander si besoins!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Combobox文本属性twoway绑定不起作用 - Combobox text property twoway binding not working 将ComboBox TwoWay绑定到属性 - Binding ComboBox TwoWay to Property 具有可观察集合的XAML Win8数据绑定TwoWay - XAML Win8 Databinding TwoWay with an Observable Collection WPF AutoCompleteBox绑定.Text与Mode = TwoWay不起作用-.NET4.0 - WPF AutoCompleteBox binding .Text with Mode=TwoWay not working - .NET4.0 到ComboBox的BindingSource和到PropertyGrid的[Browsable(false)]属性不能一起使用 - BindingSource to ComboBox and [Browsable(false)] property to PropertyGrid don't work together XAML数据绑定到常规属性 - XAML databinding to regular property 设置textBox.Text不会更新绑定双向属性吗? - Setting textBox.Text doesn't update bound twoway property? xaml Twoway绑定到PhoneApplicationPage代码后面的属性 - xaml Twoway binding to a property in PhoneApplicationPage code behind 数据绑定到 ComboBox 中的 CollectionViewSource 时如何保留 CurrentItem 的双向绑定 - How to preserve TwoWay binding of CurrentItem when databinding to CollectionViewSource in ComboBox 使用WinRt Xaml Toolkit中的ListBoxItemExtensions.IsSelected无法使用双向绑定 - Using ListBoxItemExtensions.IsSelected from WinRt Xaml Toolkit doesn't work TwoWay Binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM