绑定到系统命令的WPF窗口按钮(关闭,最大,最小,恢复)失败

时间:2017-06-29 14:46:25

标签: c# wpf data-binding

修改

我已更正输出窗口中引用的绑定问题:

  

System.Windows.Data信息:41:BindingExpression路径错误:'source {x:Static SystemCommands'属性找不到'object',因为数据项为null。这可能是因为数据提供者尚未生成任何数据。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

我在Source和{

之间错过了=

在下面引用的输出窗口的每一行都提到了这一点,但修复此问题并未解决 按钮仍然无效的问题。

输出窗口现在充满了以下内容,所有引用控件都不是我的窗口按钮:

  

System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;使用默认值。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

原始问题

基于SO的几个答案,我已经构建了以下按钮来关闭,最大化,恢复和最小化我的窗口:

<!--Close Button-->
<Button x:Name="Close" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.CloseWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <Grid Width="30" Height="25">
        <Path x:Name="Cross" Data="M0,0 L1,1 M0,1 L1,0" Stretch="Fill" Width="8" Height="8"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="2" />
    </Grid>
</Button>

<!--Maximize Button-->
<Button x:Name="Maximize" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" >
    <Grid Width="30" Height="25">
        <Path Data="M0,1 L9,1 L9,8 L0,8 Z" Width="9" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="2"  />
    </Grid>
</Button>

<!--Restore Button-->
<Button x:Name="Restore" Visibility="Collapsed" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.RestoreWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <Grid Width="30" Height="25" UseLayoutRounding="True" RenderTransform="1,0,0,1,.5,.5">
        <Path Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z" Width="8" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="1"  />
    </Grid>
</Button>

<!--Minimize Button-->
<Button x:Name="Minimize" Style="{StaticResource TitleButton}"
        Command="{Binding Source={x:Static SystemCommands.MinimizeWindowCommand}}"
        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <Grid Width="30" Height="25">
        <Path Data="M9,0 L8,1 8,1 8,1 0,1 0,1 z" Width="9" Height="9" VerticalAlignment="Bottom" HorizontalAlignment="Center"
              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="2" />
    </Grid>
</Button>

我已经与其他一些知识渊博的开发人员检查了绑定,他们说绑定看起来很好,但我可能需要一个命名空间。他们必须先行才能澄清。

Visual Studio中的输出窗口显示以下错误(矛盾的是,所有似乎都与关闭窗口按钮相关,这是唯一一个使用它的样式TitleButton处理它的人

似乎这里存在某种数据绑定问题很奇怪,因为我显然不需要将这些绑定到数据......我确实在SO上找到了一些答案,但似乎都处理其他事情:

  

System.Windows.Data信息:41:BindingExpression路径错误:'source {x:Static SystemCommands'属性找不到'object',因为数据项为null。这可能是因为数据提供者尚未生成任何数据。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

     

System.Windows.Data信息:20:由于缺少信息,BindingExpression无法检索值。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

     

System.Windows.Data信息:21:BindingExpression无法从空数据项中检索值。当分离绑定或绑定到没有值的Nullable类型时,可能会发生这种情况。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

     

System.Windows.Data信息:20:由于缺少信息,BindingExpression无法检索值。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

     

System.Windows.Data信息:21:BindingExpression无法从空数据项中检索值。当分离绑定或绑定到没有值的Nullable类型时,可能会发生这种情况。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

     

System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;使用默认值。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand};的DataItem = NULL; target元素是'Button'(Name ='Close'); target属性是'Command'(类型'ICommand')

1 个答案:

答案 0 :(得分:1)

使用静态SystemCommands类中的静态路由命令时,您不需要使用Binding,而是可以将xaml简化为以下内容。

Command =“{x:Static SystemCommands.MinimizeWindowCommand}”