我试图在我的通用Windows平台应用中处理mvvm逻辑。一切工作正常与静态绑定和observableCollections,但我卡住绑定按钮Click
事件布尔财产,理论上应该影响SplitView
的IsPaneOpen
状态。起初看起来一切看起来都很好,并且没有任何警告地构建,但不知何故它不能使Property更改可观察。 这里是我的MainPageViewModel.cs:将IsPaneOpen绑定到布尔属性问题
public class MainPageViewModel : INotifyPropertyChanged
{
public string Title
{
get
{
return "String!"; // it works
}
}
private bool _isPaneOpen; // false
public bool isPaneOpen // x:Bind to SplitViews's "IsPaneOpen" property
{
get { return _isPaneOpen; } // false
set
{
if (value != this._isPaneOpen)
{
Debug.WriteLine(_isPaneOpen); // false
_isPaneOpen = value; // false -> true
Debug.WriteLine(_isPaneOpen); // true
this.OnPropertyChanged("isPaneOpen"); // and nothing happended...
}
}
}
public void changePaneState() // x:Bind to button
{
isPaneOpen = !isPaneOpen; // true
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
而这里的MainPage.xaml中:
<StackPanel Orientation="Horizontal" Grid.Column="0">
<Button Background="#eee" Name="Back" Height="50" Width="50" FontFamily="Segoe MDL2 Assets" FontSize="22" Content="" Click="{x:Bind ViewModel.changePaneState}"/>
<TextBlock VerticalAlignment="Center"
Margin="10 0 0 0"
Name="DebugTextBlock" FontSize="18" Text="{x:Bind ViewModel.Title}"
FontWeight="SemiBold"/>
</StackPanel>
</Grid>
</Grid>
<SplitView Name="SideBar" IsPaneOpen="{x:Bind ViewModel.isPaneOpen}" Style="{StaticResource SplitViewStyle}">
<SplitView.Pane>
<Grid>
任何想法什么可能出错?
工作完美!谢谢。 – vrodis