编辑
我已经根据输出窗口中的引用修正了绑定问题:
System.Windows.Data Information: 41 : BindingExpression path error: 'Source{x:Static SystemCommands' 属性未找到,因为数据项为空。这可能是因为数据提供程序尚未生成任何数据。BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')
我在 Source 和 { 之间漏掉了一个 =
如下所述,每行输出窗口都提到了这一点,但修复此问题并没有解决按钮仍然无法使用。
输出窗口现在充满了以下内容,所有引用的控件都不是我的窗口按钮:
System.Windows.Data Information: 10 : 无法使用绑定检索值,并且不存在有效的回退值;使用默认值。BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type '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:绑定表达式路径错误:'Source{x:Static SystemCommands'属性未找到'object',因为数据项为空。这可能是因为数据提供程序尚未生成任何数据。BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand}; DataItem = null; 目标元素为'Button'(名称= 'Close'); 目标属性为“Command”(类型为'ICommand') System.Windows.Data信息:20:由于缺少信息,无法检索绑定表达式的值。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand}; DataItem = null; 目标元素为'Button'(名称= 'Close'); 目标属性为“Command”(类型为'ICommand') System.Windows.Data信息:21:无法从空数据项中检索绑定表达式的值。当绑定被分离或绑定到没有值的可空类型时,可能会发生这种情况。BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand}; DataItem = null; 目标元素为'Button'(名称= 'Close'); 目标属性为“Command”(类型为'ICommand') System.Windows.Data信息:20:由于缺少信息,无法检索绑定表达式的值。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand}; DataItem = null; 目标元素为'Button'(名称= 'Close'); 目标属性为“Command”(类型为'ICommand') System.Windows.Data信息:21:无法从空数据项中检索绑定表达式的值。当绑定被分离或绑定到没有值的可空类型时,可能会发生这种情况。BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand}; DataItem = null; 目标元素为'Button'(名称= 'Close'); 目标属性为“Command”(类型为'ICommand') System.Windows.Data信息:10:无法使用绑定检索值,也不存在有效的回退值;改为使用默认值。 BindingExpression:Path = Source {x:Static SystemCommands.CloseWindowCommand}; DataItem = null; 目标元素为'Button'(名称= 'Close'); 目标属性为“Command”(类型为'ICommand')