2009-10-12 63 views
4

對於我的生活,我似乎無法使用多重綁定綁定到我的視圖模型。網上的所有示例直接綁定到gui元素,但是每當我嘗試使用viewmodel對象引發異常時。wpf multibinding viewmodel?

我的問題是,如何添加一個multibinding到xaml中的幾個viewmodel對象?

我需要將上下文菜單的IsEnabled屬性綁定到我的viewmodel中的兩個整數。以下綁定不起作用,因爲它是爲GUI組件設計的。我該如何處理我的整數?

<MenuItem ItemsSource="{Binding MyMenuItem}"> 
    <MenuItem.IsEnabled> 
     <MultiBinding> 
      <Binding ElementName="FirstInt" Path="Value" /> 
      <Binding ElementName="SecondInt" Path="Value" /> 
     </MultiBinding> 
    </MenuItem.IsEnabled> 
</MenuItem> 

MyMenuItem是具有兩個整數FirstInt和SecondInt的CLR對象。

+3

你的問題是什麼? – Natrium 2009-10-12 07:50:21

+1

給出更多的細節或源代碼來重現您的問題 – japf 2009-10-12 08:14:08

回答

3

對於您的特定示例,您需要一個IMultiValueConverter,它將兩個整數轉換爲表示菜單項是否啓用的布爾值。事情是這樣的:

Public Class MVCIntsToEnabled 
    Implements IMultiValueConverter 

    Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert 
     If values IsNot Nothing Then 
      If values.Count = 2 Then 
       Return (values(0) > 0) AndAlso (values(1) > 0) 
      Else 
       Return False 
      End If 
     Else 
      Throw New ArgumentNullException("values") 
     End If 
    End Function 

    Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack 
     Throw New NotImplementedException() 
    End Function 

End Class 

像這樣來使用:

<local:MVCIntsToEnabled x:Key="IntsToEnabledConverter" /> 

... 

<MenuItem ItemsSource="{Binding MyMenuItem}"> 
    <MenuItem.IsEnabled> 
     <MultiBinding Converter="{StaticResource IntsToEnabledConverter}"> 
      <Binding ElementName="FirstInt" Path="Value" /> 
      <Binding ElementName="SecondInt" Path="Value" /> 
     </MultiBinding> 
    </MenuItem.IsEnabled> 
</MenuItem> 
3

這是舊的文章,但我知道我有同樣的問題,無法在網絡上找到任何解決方案。

我認爲他問的是如何使用不綁定到GUI元素的多重綁定,而是綁定到視圖模型中的多個屬性。

事情是這樣的:

的XAML:

<MenuItem ItemsSource="{Binding MyMenuItem}"> 
    <MenuItem.IsEnabled> 
     <MultiBinding Converter="{StaticResource IntsToEnabledConverter}"> 
      <Binding Source="{Binding FirstInt}" /> 
      <Binding Source="{Binding SecondInt}" /> 
     </MultiBinding> 
    </MenuItem.IsEnabled> 
</MenuItem> 

視圖模型:

public class ViewModel 
{ 
public int FirstInt{ get { return _firstInt;}} 
public int SecondInt{ get { return _secondInt;}} 

} 

我一直沒能要麼想出解決辦法。相反,我用了一個SingleValueConverter和綁定到持有父對象兩個變量FirstInt和SecondInt和轉換器使用這個父,水木清華這樣的:

public class IntsToEnabledConverter :IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      Parent parent = value as Parent; 

      if (parent.FirstInt == 1 && parent.SecondInt == 1) 
       return true; 
      else 
       return false; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

這不乾淨,就好像一個Multibinding作爲家長可能成爲擁有更多成員的更大對象,但它對我來說很有效。如果我可以使用Multibinding解決方案,我會更好地查看代碼。

+0

綁定綁定的來源會產生運行時異常。 – Brannon 2015-03-31 16:33:08

1

Bluebit你的等待已經結束了,而是爲你的目標控件創建一個樣式。

這裏是一個例子,其中我有一個按鈕,這是需要有如果一個組合框(命名comboConfig)不選擇尚未被禁用的登錄按鈕(選擇的索引-1),或者如果一個布爾值在我的ViewModel上設置爲true(LoginInProcess)。對於視圖模型布爾我只是設置爲成員名稱屬性的路徑,這是在風格的時間到窗口的DataContext綁定:

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> 
    <Style.Triggers> 
     <DataTrigger Value="True"> 
      <DataTrigger.Binding> 
       <MultiBinding Converter="{StaticResource MultiComboBoolBoolFalse}"> 
        <Binding ElementName="comboConfig" Path="SelectedIndex" /> 
        <Binding Path="LoginInProcess"/> 
       </MultiBinding> 
      </DataTrigger.Binding> 
      <Setter Property="IsEnabled" Value="False"/> 
     </DataTrigger> 
    </Style.Triggers> 
</Style> 
11

菲利普的回答是可以接受的,但對於那些尋找菲利普期望的解決方案,下面應該這樣做:

<MenuItem ItemsSource="{Binding MyMenuItem}"> 
    <MenuItem.IsEnabled> 
     <MultiBinding Converter="{StaticResource IntsToEnabledConverter}"> 
      <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="DataContext.FirstInt" /> 
      <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="DataContext.SecondInt" /> 
     </MultiBinding> 
    </MenuItem.IsEnabled> 
</MenuItem> 

應當指出的是,在結合AncestorType可能需要相應改變。我認爲視圖模型設置爲窗口的DataContext,但同樣的想法適用於用戶控件等。

+0

這應該是接受的答案。如果它不適用於您,請嘗試向兩個綁定明確添加'NotifyOnSourceUpdated =「True」'。 – Artholl 2014-12-11 13:43:17