我正在尝试在我的通用 Windows 平台应用程序中处理 mvvm 逻辑。静态绑定(bind)和 observableCollections 一切正常,但我陷入了将 Button Click
事件绑定(bind)到 bool 属性的困境,理论上这应该影响 SplitView
的 IsPaneOpen
状态。乍一看,一切看起来都很好,并且在没有任何警告的情况下构建,但不知何故,它无法使属性更改可观察到。
这是我的 MainPageViewModel.cs:
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>
有什么想法可能会出错吗?
最佳答案
模式应该是 TwoWay
,因为您正在 Viewmodel 中进行更改,为了反射(reflect)在 Ui 中,您应该指定 TwoWay
模式
IsPaneOpen="{x:Bind ViewModel.isPaneOpen,Mode=TwoWay}"
关于c# - 绑定(bind) IsPaneOpen 解决 bool 属性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862264/