使用命令更改xaml属性

时间:2017-02-11 09:19:31

标签: c# xaml mvvm uwp

我想用命令打开和关闭splitview.pane。这是我的示例xaml-code:

<SplitView Name="AppNavigation" DisplayMode="CompactOverlay" IsPaneOpen="False" CompactPaneLength="50" OpenPaneLength="200">
    <SplitView.Pane>
        <Button Name="Hamburger" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" FontSize="24" Width="50" Height="50" Command="{Binding HamburgerExecute}" />
    </SplitView.Pane>
</SplitView>

我使用RelayCommand类并调用这两个方法:

    private bool HamburgerCanExecute(object obj)
    {
        return true;
    }

    private void HamburgerExecute(object obj)
    {
        AppNavigation.IsPaneOpen = !AppNavigation.IsPaneOpen; // this doesn't work
    }

有人可以向我解释如何使用命令更改xaml属性吗?

3 个答案:

答案 0 :(得分:0)

您似乎直接将该方法绑定到Button,但您声明使用的是RelayCommand。请确保您绑定RelayCommand而不是实际方法。

希望它有所帮助!

答案 1 :(得分:0)

在按钮中创建点击事件。

poly
: 
     (TLK^ term)*
; 

C#:

<SplitView Name="AppNavigation" DisplayMode="CompactOverlay" IsPaneOpen="False" CompactPaneLength="50" OpenPaneLength="200">
<SplitView.Pane>
    <Button Name="Hamburger" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" FontSize="24" Width="50" Height="50" Click="Hamburger_Click />
</SplitView.Pane>

答案 2 :(得分:0)

我忘了整合UI名称空间......该死的

所以正确的代码是:

private void HamburgerExecute(object obj)
{
    SplitView navigation = obj as SplitView;
    navigation.IsPaneOpen = !navigation.IsPaneOpen;
}

我的xaml文件中的CommandParameter:

<SplitView Name="AppNavigation" DisplayMode="CompactOverlay" IsPaneOpen="False" CompactPaneLength="50" OpenPaneLength="200">
    <SplitView.Pane>
        <Button Name="Hamburger" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" FontSize="24" Width="50" Height="50" Command="{Binding HamburgerCommand}" CommandParameter="{Binding ElementName=AppNavigation}" />
    </SplitView.Pane>
</SplitView>