繁体   English   中英

C#UWP将splitview IsPane绑定到viewmodel

[英]C# UWP binding splitview IsPaneOpen to viewmodel

遇到此问题时,我正在学习通用Windows应用程序上的一些知识:我想构建一个带有汉堡按钮的splitview菜单,而无需编写代码。 所以我设置了一个带属性的viewmodel和一个用于更改该属性值的命令。 为了检查命令是否被解雇,我提供了一条小消息对话框。 我将splitview IsPaneOpen绑定到了我的viewmodel,但是以某种方式似乎不起作用。

XAML代码

<Page.Resources>
    <vm:PaneViewModel x:Key="viewModel"/>
</Page.Resources>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource viewModel}" >
    <Button Content="Test" Command="{Binding Path=OpenPane, Mode=TwoWay}" ManipulationMode="All"/>
    <SplitView DisplayMode="CompactInline" 
               x:Name="Splitview"
               OpenPaneLength="150" 
               CompactPaneLength="20" 
               IsPaneOpen="{Binding IsPaneOpen, Mode=TwoWay}">
        <SplitView.Pane>
            <StackPanel Height="400">
                <Button Height="40">
                    <TextBlock Text="Testbutton" Width="100"/>
                </Button>
            </StackPanel>
        </SplitView.Pane>
        <SplitView.Content>
            <TextBlock Margin="30" Text="{Binding ShowAnything, Mode=TwoWay}"/>
        </SplitView.Content>
    </SplitView>
</StackPanel>

ViewModel代码

    internal class PaneViewModel
{
    public PaneViewModel()
    {
        OpenPane = new OpenPaneCommand(this);
    }
    private bool isPaneOpen = true;
    public bool IsPaneOpen
    {
        get
        {
            return isPaneOpen;
        }

        set
        {
            isPaneOpen = value;
        }
    }
    public string ShowAnything { get { return isPaneOpen.ToString(); } }
    public OpenPaneCommand OpenPane { get; set; }

    public void OpenPaneMethod()
    {
        if (isPaneOpen == false)
            isPaneOpen = true;
        else
            isPaneOpen = false;
    }
}

和命令代码

    internal class OpenPaneCommand : ICommand
{
    public OpenPaneCommand(PaneViewModel ViewModel)
    {
        this.viewModel = ViewModel;
    }
    private PaneViewModel viewModel;

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        Blub();
        viewModel.OpenPaneMethod();
    }
    async private void Blub()
    {
        var dialog = new MessageDialog("Testausgabe");
        await dialog.ShowAsync();
    }

就像我说的-消息对话框出现了,但是splitview.content或ispaneopen中的文本块似乎都没有改变。 调试告诉我,我更改值的方法的确确实会更改值。 所以我想知道我的绑定或数据上下文设置是否已关闭。

也许你们对我有暗示,我的误会来自哪里。

谢谢!

梅克

您的绑定无法正常工作,因为没有通知您ViewModel的更改。 您的VM需要实现INotifyPropertyChanged接口,并在属性更改时引发PropertyChanged事件。

因此,通常您会将这样的内容放入设置器中:

...
set 
{
   isPaneOpen = value;
   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsPaneOpen");
}
...

我认为您的视图模型应该继承ObservableObject 并进一步更新所有属性,如下所示:

 public bool IsPaneOpen
        {
            get { return isPaneOpen; }
            set
            {
                this.Set<bool>(ref this._loisPaneOpendedPendingCount, value);
            }
        }

ShowAnything属性相同

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM