【发布时间】:2017-12-03 08:19:11
【问题描述】:
编辑
我已更正了输出窗口中引用的绑定问题:
System.Windows.Data 信息:41:BindingExpression 路径错误:找不到“object”的“Source{x:Static SystemCommands”属性,因为数据项为空。这可能是因为数据提供者尚未生成任何数据。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“命令”(输入“ICommand”)
我错过了 Source 和 {
之间的=
在下面引用的输出窗口的每一行中都提到了这一点,但修复此问题并没有解决 按钮仍然不执行任何操作的问题。
输出窗口现在充满了以下内容,所有引用控件不是我的窗口按钮:
System.Windows.Data 信息:10:无法使用绑定检索值,并且不存在有效的备用值;改用默认值。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“命令”(输入“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 路径错误:找不到“object”的“Source{x:Static SystemCommands”属性,因为数据项为空。这可能是因为数据提供者尚未生成任何数据。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“命令”(输入“ICommand”)
System.Windows.Data 信息:20:由于缺少信息,BindingExpression 无法检索值。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“命令”(输入“ICommand”)
System.Windows.Data 信息:21:BindingExpression 无法从空数据项中检索值。当绑定被分离或绑定到没有值的 Nullable 类型时,可能会发生这种情况。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“命令”(输入“ICommand”)
System.Windows.Data 信息:20:由于缺少信息,BindingExpression 无法检索值。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“命令”(输入“ICommand”)
System.Windows.Data 信息:21:BindingExpression 无法从空数据项中检索值。当绑定被分离或绑定到没有值的 Nullable 类型时,可能会发生这种情况。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“命令”(输入“ICommand”)
System.Windows.Data 信息:10:无法使用绑定检索值,并且不存在有效的备用值;改为使用默认值。 BindingExpression:Path=Source{x:Static SystemCommands.CloseWindowCommand};数据项=空;目标元素是“按钮”(名称=“关闭”);目标属性是“Command”(输入“ICommand”)
【问题讨论】:
标签: c# wpf data-binding