2014-02-19 100 views
3

當在ComboBox的SelectedValue屬性上調用該函數時,我發現了一個奇怪的行爲GetBindingExpression()GetBindingExpression在第二次調用時返回空值

它在第一次調用時工作正常,但在返回的BindingExpression上調用UpdateTarget()之後,此BindingExpression似乎從屬性中刪除。下次來自屬性的GetBindingExpression()返回null。

BindingExpression exp; 

// call it first time, returns an expression, and UpdateTarget success. 
exp = ((ComboBox)obj).GetBindingExpression(ComboBox.SelectedValueProperty); 

// call UpdateTarget 
if (exp != null) exp.UpdateTarget(); 

// call it the second time, and now it returns null! 
exp = ((ComboBox)obj).GetBindingExpression(ComboBox.SelectedValueProperty); 

if (exp != null) exp.UpdateTarget(); 

如果我刪除UpdateTarget(的通話),那麼接下來GetBindingingExpression()仍然會返回一個有效的BindingExpression。

這種問題不會發生在CheckBox的IsChecked屬性上。它總是可以正常使用CheckBox的IsChecked屬性。

的XAML是象下面這樣:

<Style TargetType="{x:Type ComboBox}" x:Key="GainComboBoxStyle_0x00000022"> 
    <Setter Property="Height" Value="20" /> 
    <Setter Property="MaxWidth" Value="80" /> 
    <Setter Property="FontFamily" Value="Calibri"/> 
    <Setter Property="FontSize" Value="15"/> 
    <Setter Property="FontWeight" Value="UltraBold"/> 
    <Setter Property="Tag" Value="{StaticResource moduleGain_0x00000022}" /> 
    <Setter Property="Visibility" Value="{Binding Mode=OneWay, Converter={StaticResource getVisibilityOfGainConverter}}"/> 

    <Style.Triggers> 
     <DataTrigger Binding="{Binding Mode=OneWay, Converter={StaticResource isShowingGain}}" Value="True"> 
      <Setter Property="SelectedValue" Value="{Binding Path=., RelativeSource={RelativeSource Mode=Self}, Mode=OneWay, Converter={StaticResource getGainValue}}"/> 
      <Setter Property="DisplayMemberPath" Value="Name" /> 
      <Setter Property="SelectedValuePath" Value="Name" /> 
      <Setter Property="IsSynchronizedWithCurrentItem" Value="False" /> 
      <Setter Property="ItemsSource" Value="{Binding Path=GainQ13Indices, Mode=OneTime}" /> 
     </DataTrigger> 
    </Style.Triggers> 
    </Style> 

    <Style TargetType="{x:Type CheckBox}" x:Key="EnableCheckBoxStyle_0x00000010"> 
    <Setter Property="MaxWidth" Value="16"/> 
    <Setter Property="Tag" Value="{StaticResource 
         moduleEnable_0x00000010}" /> 
    <Setter Property="Visibility" Value="{Binding Mode=OneWay, Converter={StaticResource getVisibilityOfEnableConverter}}"/> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding Mode=OneWay, Converter={StaticResource isShowingEnable}}" Value="True"> 
      <Setter Property="IsChecked" Value="{Binding Path=., RelativeSource={RelativeSource 
              Mode=Self}, Mode=OneWay, 
              Converter={StaticResource 
              moduleEnableDisableConverter} }" /> 
     </DataTrigger> 
    </Style.Triggers> 
    </Style> 

爲什麼UpdateTarget()從組合框的SelectedValue屬性擦除BindingExpression?以及爲什麼它可以正常工作CheckBox的IsChecked屬性?

+0

是對項仍選擇的第二陣子? –

+0

你可以用SelectedItem改爲SelectedValue嗎? –

+0

是的,它仍然被選中,因爲你可以看到兩個呼叫是並排的。第一個成功,第二個得到空。我無法使用SelectedItem,因爲BindingSource僅用作輸入,並由未知數量的ComboBoxes共享來初始化它們的列表。綁定是OneTime,否則組合框將互相干擾。 – user1943928

回答

1

確保您的SelectedValue被設置爲GainQ13Indices中包含的實際對象之一 - 看起來您可能會在轉換器中生成一個對象,這會導致綁定中斷。預計SelectedValue被設置爲綁定到ItemsSource的集合中包含的內容。

我猜測它適用於IsChecked,因爲這是一個布爾值,它是一個值類型,而它看起來像你在ComboBox中使用的是一個引用類型(其中相等性檢查有點涉及) 。

我以前遇到過這類問題,並且習慣於在視圖模型中公開一個額外的屬性來表示每個列表的選定值,並且避免嘗試執行任何操作篡改代碼中的綁定。

例如(在視圖模型):

public ObservableCollection<Gain> GainQ13Indices { get; private set; } 

public Gain SelectedGainQ13Indice { get; set; } 
+0

這是否意味着如果UpdateTarget()失敗,那麼BindingExpression被刪除?但似乎UpdateTarget()第一次成功,因爲我確實看到它在GUI上相應地更新。如果由於某些錯誤而被刪除,那麼我可以重置它嗎?我正在尋找方法來重置它。 GainQ13Indices也由多個(編譯時未知的數字)組合框共享。這就是爲什麼我使用OneTime綁定在開始時獲取列表並使用Converter爲每個ComboBox決定SelectedValue,否則它們將互相干擾。 – user1943928

+1

是的,它第一次成功了,但可能是因爲它做了一些無效的事情(比如將SelectedItem設置爲不包含在「allowed items」/ ItemsSource中的東西),該值直接分配給屬性,就好像您已經設置了它通過屬性設置器,它不再通過綁定來抽象 - 綁定被有效地替換爲固定值。這仍然會在GUI中顯示出來,但由於它是一個固定值並且不再是綁定表達式,因此要求綁定表達式返回null。也許你的轉換器(getGainValue)正在創建一個新的對象? –

相關問題