绑定 IsPaneOpen 到 bool 属性 麻烦
Binding IsPaneOpen to bool property trouble
我正在尝试处理我的通用 Windows 平台应用程序中的 mvvm 逻辑。静态绑定和 observableCollections 一切正常,但我无法将 Button Click
事件绑定到 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>
有什么可能出错的想法吗?
Mode 应该是 TwoWay
,因为你在 Viewmodel 中进行更改,要反映在 Ui 你应该给 TwoWay
Mode
IsPaneOpen="{x:Bind ViewModel.isPaneOpen,Mode=TwoWay}"
x:绑定模式默认为OneTime(经典绑定为OneWay)。所以需要显式设置绑定模式为OneWay或TwoWay。
IsPaneOpen="{x:Bind ViewModel.isPaneOpen,Mode=OneWay}"
我正在尝试处理我的通用 Windows 平台应用程序中的 mvvm 逻辑。静态绑定和 observableCollections 一切正常,但我无法将 Button Click
事件绑定到 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>
有什么可能出错的想法吗?
Mode 应该是 TwoWay
,因为你在 Viewmodel 中进行更改,要反映在 Ui 你应该给 TwoWay
Mode
IsPaneOpen="{x:Bind ViewModel.isPaneOpen,Mode=TwoWay}"
x:绑定模式默认为OneTime(经典绑定为OneWay)。所以需要显式设置绑定模式为OneWay或TwoWay。
IsPaneOpen="{x:Bind ViewModel.isPaneOpen,Mode=OneWay}"