gpt4 book ai didi

c# - WPF 窗口按钮(关闭、最大、最小、恢复)绑定(bind)到系统命令失败

转载 作者:行者123 更新时间:2023-12-05 06:39:14 25 4
gpt4 key购买 nike

编辑

我已经更正了输出窗口中引用的绑定(bind)问题:

System.Windows.Data Information: 41 : BindingExpression path error: 'Source{x:Static SystemCommands' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. 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 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. 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>

我已经与其他一些知识渊博的开发人员一起检查了绑定(bind),他们说绑定(bind)看起来不错,但也许我需要一个 namespace 。在他们能够澄清之前,他们必须离开。

Visual Studio 中的输出窗口显示以下错误(自相矛盾的是,所有这些似乎都与关闭窗口按钮相关,这是唯一具有 TitleButton 样式的按钮正在努力。

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

System.Windows.Data Information: 41 : BindingExpression path error: 'Source{x:Static SystemCommands' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand}; DataItem=null; target element is 'Button' (Name='Close'); target property is 'Command' (type 'ICommand')

最佳答案

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

Command="{x:Static SystemCommands.MinimizeWindowCommand}"

关于c# - WPF 窗口按钮(关闭、最大、最小、恢复)绑定(bind)到系统命令失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44828381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com