c# - 绑定(bind) IsPaneOpen 解决 bool 属性问题

标签 c# mvvm win-universal-app windows-10-universal

我正在尝试在我的通用 Windows 平台应用程序中处理 mvvm 逻辑。静态绑定(bind)和 observableCollections 一切正常,但我陷入了将 Button Click 事件绑定(bind)到 bool 属性的困境,理论上这应该影响 SplitViewIsPaneOpen 状态。乍一看,一切看起来都很好,并且在没有任何警告的情况下构建,但不知何故,它无法使属性更改可观察到。 这是我的 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="&#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>

有什么想法可能会出错吗?

最佳答案

模式应该是 TwoWay,因为您正在 Viewmodel 中进行更改,为了反射(reflect)在 Ui 中,您应该指定 TwoWay 模式

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

关于c# - 绑定(bind) IsPaneOpen 解决 bool 属性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36862264/

相关文章:

c# - 使用两个参数 (string, int) 定义字符串输出中特定字符的最大数量

c# - DataGridTemplateColumn 绑定(bind)

c# - 在哪里使用 MVVM 中的 Web 服务?

windows-phone-8.1 - 使用 Windows 通用应用程序在设备之间同步数据

c - 未处理的异常 0xC0000008 : An invalid handle was specified in dynamic recompiler

c# - .NET 依赖管理和标记/分支

c# - 在 C# 中显示后立即关闭窗体

android - `NoClassDefFoundError: android.databinding.DataBindingUtil` 运行带有数据绑定(bind)的 fragment 的 android 测试时

c# - W10 UWP - 将远程图像设置为桌面墙纸/锁屏

c# - 在 C# 控制台应用程序中播放 mp3 声音