我目前正在使用MVVM(而不是MVVM Light)在C#/ XAML中開發通用應用程序,並且我在XAML部分中遇到了問題。當依賴項屬性發生變化時,StackPanel可見性未更新
我想顯示一個或另一個StackPanel
當我的ViewModel中的依賴項屬性發生變化。我認爲這個代碼本身就是有說服力的。
<StackPanel Visibility="{Binding MyProperty, Converter={StaticResource BooleanToVisibilityConverter}}">
<!-- Some content -->
</StackPanel>
<StackPanel Visibility="{Binding MyProperty, Converter={StaticResource InvertBooleanToVisibilityConverter}}">
<!-- Some another content -->
</StackPanel>
這裏是依賴項屬性的定義。
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
"MyProperty",
typeof (bool),
typeof (MyViewModel),
new PropertyMetadata(true));
public bool MyProperty
{
get { return (bool) GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); OnPropertyChanged(); // Implemented by ReSharper }
}
我猜你弄明白這MyProperty
是我通過轉換器轉換爲Visibility
一個布爾值。所以,當在ViewModel中更改MyProperty
時,視圖不會更新。
我已經嘗試使用UpdateSourceTrigger
屬性,但它不起作用。此外,我沒有綁定錯誤和轉換器工作正常(我只看到一個StackPanel
在應用程序啓動)。
請記住,我不想使用部分背後的代碼,除非沒有其他解決方案。
感謝您的幫助。
您可以添加您的依賴屬性的定義嗎? – 2014-10-28 20:11:48
你通知財產改變了嗎? – 2014-10-28 20:14:19
@AaronHawkins:當然。我剛剛編輯了首發帖子。 – Atlasmaybe 2014-10-28 20:41:55