代码之家  ›  专栏  ›  技术社区  ›  Service Informatique

XAML—为什么在.NET4.0中,双向数据绑定对组合框的文本属性不起作用?

  •  5
  • Service Informatique  · 技术社区  · 14 年前

    为什么双向数据绑定在.NET4.0中对组合框的文本属性不起作用(它在.NET3.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 使用auto completion为每个元素使用不同的自定义选项,但是我可以编写我想要的内容,结果是在元素的attribute选项中 <combobox>

    为什么?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Pascal Qyy    14 年前

    好吧,我要你解决这个问题!这是一次游行。

    与此相对应的是,这是一个由3.5年的框架和4.0年的框架组成的电话变更。。。

    下面是使用framework4.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;
        }
    }
    

    为了理解,请看一下:

        2
  •  1
  •   NullUserException Mark Roddy    13 年前

    目前,我发现这个问题唯一真正的解决方案不是针对framework4.0,而是针对framework3.5。。。