2010-02-10 29 views
11

我寫了一個MultiValueConverter,它檢查給定列表是否包含給定值,如果有,則返回true。我用它來綁定到自定義複選框列表。現在我想編寫ConvertBack方法,這樣如果複選框被選中,原始值將被髮送到模型。有沒有辦法在ConvertBack方法中訪問值?WPF:有沒有辦法在MultiValueConverter的ConvertBack方法中獲取原始值?

XAML:

<ListBox.ItemTemplate> 
    <HierarchicalDataTemplate> 
     <CheckBox Content="{Binding Path=Description}"> 
      <CheckBox.IsChecked> 
       <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}"> 
        <Binding Path="Id" /> 
        <Binding Path="DataContext.ContactTypes" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" /> 
       </MultiBinding> 
      </CheckBox.IsChecked> 
     </CheckBox> 
    </HierarchicalDataTemplate> 
</ListBox.ItemTemplate> 

我得到時,我結合正確的結果,但有沒有辦法轉換回時,爲了獲得綁定ID?我想實現的是,如果複選框未被選中,該值將從列表中刪除,如果選中,該值將被添加到列表中。

+0

我有一個類似的問題,其中多重綁定中的一個綁定是一個包含文本字段的對象列表的對象。多重綁定基於其他綁定值綁定到其中一個文本框。我需要文本框來更改convertBack上的對象的文本,但我擁有的僅僅是新的值,而不是對象的文本需要更改 – JoeSharp 2010-05-20 19:29:35

回答

5

我解決了我的問題,希望解決方案也能幫助你。採取你有的多重綁定,而不是把它放在IsChecked屬性中,把它放在DataContext屬性中。這可能是我們的問題不同的地方......在我的convert方法中,我使用綁定中的數據來獲取對象,然後返回myobject.text。我將其改爲僅返回對象,以便將其綁定到datacontext。然後,我將textbox.text屬性綁定到myobject的text屬性。它似乎工作正常。然後,您可以將刪除值的列表綁定到checkbox.ischecked ...我想。我不確定你想要做什麼。

我想這可能讓你在正確的道路上......

<ListBox.ItemTemplate> 
<HierarchicalDataTemplate> 
    <CheckBox Content="{Binding Path=Description}"> 
     <CheckBox.DataContext> 
      <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}"> 
       <Binding Path="Id" /> 
       <Binding Path="DataContext.ContactTypes" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" /> 
      </MultiBinding> 
     </CheckBox.DataContext> 
     <CheckBox.IsChecked> 
      <Binding Path="Some_Property_For_IsChecked_In_Some_Object_In_The_Converter" /> 
     </CheckBox.IsChecked> 
    </CheckBox> 
</HierarchicalDataTemplate> 

6

我知道這是一個老問題,但這種解決方案可以幫助別人。

代替使用IMultiValueConverter的所述ConvertBack方法的,可以設置IsChecked結合是OneWay,並使用CheckBoxCommand屬性來執行檢查的邏輯。

<ListBox.ItemTemplate> 
    <HierarchicalDataTemplate> 
     <CheckBox Content="{Binding Path=.}" Command="{Binding Path=CheckBoxCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParameter="{Binding Path=.}"> 
      <CheckBox.IsChecked> 
       <MultiBinding Converter="{x:Static Classes:ListContainsMultiConverter.Instance}" Mode="OneWay"> 
        <Binding Path="." /> 
        <Binding Path="SelectedItems" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" /> 
       </MultiBinding> 
      </CheckBox.IsChecked> 
     </CheckBox> 
    </HierarchicalDataTemplate> 
</ListBox.ItemTemplate> 

然後,添加執行類似於這樣的CheckBoxCommand:

// the items bound to your checkbox 
    public Collection<string> Items { get; set; } 

    // the list of selected items 
    public Collection<string> SelectedItems { get; set; } 

    private void ExecuteCheckBoxCommand(string obj) 
    { 
     SelectedItems.Add(obj); 
    } 

我知道這是一個稍微迂迴的這樣的方式,但IMultiValueConverter.ConvertBack方法是真的很沒用 - 我能」如果沒有訪問當前的綁定值,就會想到它太多的用法。

+3

感謝這種方法,這對我來說很有用。我花了一些時間才意識到ConvertBack完全沒用。他們應該把它放在文件中,並且一段時間都省下來。 – Jan 2012-06-07 15:59:42

+0

是完全無用:( – Asheh 2015-12-10 18:20:45

相關問題