XAML - 为什么数据绑定 TwoWay 不适用于 .net 4.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是使用框架 4.0 执行此代码的解决方案(我尝试将其适应您的示例,但我不确定。无论如何,这就是想法):
更改您的
ListItem
控件像这样:你的新代码背后是:
为了理解,请看一下:
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:And your new code behind is:
For understand, take a look at:
目前,我发现这个问题唯一真正的解决方案是不针对 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...