WPF窗口按钮(关闭,最大,最低,还原)绑定到系统命令失败



编辑

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

system.windows.data信息:41:bindingExpression路径错误:'source {x:static systemcommands的属性找不到'对象',因为数据项为null。这可能会发生,因为数据提供商尚未产生任何数据。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(

我错过了源和{

之间的=

在输出窗口的每一行中提到了这一点,但是解决此问题尚未纠正按钮仍然什么都不做的问题。

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

system.windows.data信息:10:无法使用绑定且不存在有效的回载值来检索值;改用默认值。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(


原始问题

基于一些答案,我构建了以下按钮以关闭,最大化,还原和最小化我的窗口:

<!--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的窗口。

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

  • 如何根据属性隐藏wpf datagrid列
  • 元素名称绑定失败
  • system.windows.data信息:41:bindingExpression路径错误:'source {x:static systemcommands的属性找不到'对象',因为数据项为null。这可能会发生,因为数据提供商尚未产生任何数据。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(

    system.windows.data信息:20:由于缺少信息,绑定表达无法检索值。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(

    system.windows.data信息:21:绑定表达无法从空数据项中检索值。当绑定分离或与无值的无效类型结合时,这可能会发生。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(

    system.windows.data信息:20:由于缺少信息,绑定表达无法检索值。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(

    system.windows.data信息:21:绑定表达无法从空数据项中检索值。当绑定分离或与无值的无效类型结合时,这可能会发生。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(

    system.windows.data信息:10:无法使用绑定且不存在有效的回载值来检索值;改用默认值。bindingexpression:path = source {x:static systemcommands.closewindowcommand};dataitem = null;目标元素是"按钮"(name ='Close'(;目标属性为"命令"(键入'icommand'(

    使用在静态systemcommands类中找到的静态路由命令时,您不需要使用绑定,可以简化XAML。

    命令=" {x:static systemcommands.minimizewindowcommand}"

    最新更新