2016-04-26 60 views
2

我试图在我的通用Windows平台应用中处理mvvm逻辑。一切工作正常与静态绑定和observableCollections,但我卡住绑定按钮Click事件布尔财产,理论上应该影响SplitViewIsPaneOpen状态。起初看起来一切看起来都很好,并且没有任何警告地构建,但不知何故它不能使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="&#xE0D5;" 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> 

任何想法什么可能出错?

回答

2

方式应该是TwoWay,因为你是在视图模型做出变化,在UI,以反映你应该给TwoWay模式

IsPaneOpen="{x:Bind ViewModel.isPaneOpen,Mode=TwoWay}" 
+2

工作完美!谢谢。 – vrodis

0

X:绑定模式是默认一次性(经典的结合是单向的) 。所以你需要明确地将绑定模式设置为OneWay或TwoWay。

IsPaneOpen="{x:Bind ViewModel.isPaneOpen,Mode=OneWay}" 
相关问题