這是舊的文章,但我知道我有同樣的問題,無法在網絡上找到任何解決方案。
我認爲他問的是如何使用不綁定到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解決方案,我會更好地查看代碼。
你的問題是什麼? – Natrium 2009-10-12 07:50:21
給出更多的細節或源代碼來重現您的問題 – japf 2009-10-12 08:14:08