2014-03-19 104 views
0

本质上,我想使用组合框作为选择要查看的项目的方式,而不是更新当前正在查看的项目。有没有可能使用绑定而不是代码来做到这一点?如何使wpf组合框不更新项目中的项目

下面是我在视图代码:

<ComboBox DisplayMemberPath="CustomerId" SelectedValuePath="CustomerId" 
SelectedValue="{Binding Path=CustomerId}" ItemsSource="{Binding Mode=OneWay}" 
IsSynchronizedWithCurrentItem="True"/> 

它会显示下一个成员,我从下拉框中选择,但它也将覆盖从我切换了客户的前值。

(另外,我对任何可怜的礼仪道歉,这是我在这里的第一篇文章)

编辑:为了澄清,该窗口的层次结构如下所示:包含文本框格栅和组合框和具有约束力。文本框绑定到“客户”类的各种属性,然后组合框设置选定的客户。

所以这样的:

<Grid DataContext = "{Binding Customers}"> 
    <TextBox .../> 
    ... 
    <ComboBox (see code above)/> 
</Grid> 

回答

0

要回答我自己的问题在这一个,哦。

为了解决这个问题,我用这对我的代码:

 <ComboBox 
      DisplayMemberPath="CustomerId" 
      SelectedValuePath="CustomerId" 
      SelectedValue="{Binding Path=CustomerId, Mode=OneWay, UpdateSourceTrigger=Explicit}" 
      ItemsSource="{Binding Mode=OneWay}" 
      IsSynchronizedWithCurrentItem="True"/> 

如果我不指定将选择值的结合,是一个双向绑定,它就会更新源。我认为项目源中的单向绑定应该为我做到这一点,但显然情况并非如此。

0

试试这个

<ComboBox DisplayMemberPath="CustomerId" SelectedValuePath="CustomerId" 
SelectedValue="{Binding Path=CustomerId, UpdateSourceTrigger=Explicit}" ItemsSource="{Binding Mode=OneWay}" 
IsSynchronizedWithCurrentItem="False"/> 

所以,你必须明确地更新您的SelectedValue属性中的代码更新。

+0

当我改变选择,它仍然改变对象的值。我甚至尝试添加UpdateSourceTrigger = Explicit到itemsSource绑定,这似乎也没有帮助。 – Taugenichts

+0

在xaml中使用IsSynchronizedWithCurrentItem =“False”。 我已经更新了我的答案。 – Ahmed

+0

然后它不会将网格的其余部分同步到所选项目。它不会更新项目,但它也不会更改绑定。 我更新了我的原稿以显示它的工作原理。 – Taugenichts