XAML - 为什么数据绑定 TwoWay 不适用于 .net 4.0 中组合框的文本属性?

发布于 2024-09-17 10:32:47 字数 2237 浏览 6 评论 0原文

为什么数据绑定 TwoWay 不适用于 .net 4.0 中组合框的文本属性(它适用于 .net 3.5)?

我的代码:

我有一个像这样的 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>

我有一个像这样的 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>

这是背后的代码:

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");
}

我确实喜欢这样,因为我可以有一个 ComboBox具有自动完成功能,每个选项都有不同的自定义选项,但我可以编写我想要的内容,结果位于元素 的属性选项中,

如果我的目标是 .net,它可以正常工作3.5,但如果我的目标是 .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)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-09-24 10:32:47

这是使用框架 4.0 执行此代码的解决方案(我尝试将其适应您的示例,但我不确定。无论如何,这就是想法):

更改您的 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>

你的新代码背后是:

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;
    }
}

为了理解,请看一下:

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:

纵情客 2024-09-24 10:32:47

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

For the moment, the only real solution i found to this problem is to not target Framework 4.0 but Framework 3.5...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文