wpf 中的自定义命令绑定

本文关键字:命令 绑定 自定义 wpf | 更新日期: 2023-09-27 18:18:18

VS 中的 xaml 编辑器一直给我"无效标记"消息。

我添加了一个类自定义命令.cs,在 xaml 中添加了Windows.CommandBindings等,但VS中的输出错误是:

[失败] 找不到文件 'c:''users''bart''documents''visual studio 2015''Projects''examentest200''examentest200''CustomCommand.cs'.

同时,如果我在资源管理器中检查文件,它就在那里。

提前致谢

"自定义命令.cs">

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace examentest200
{
public static class CustomCommands
{
    public static readonly RoutedUICommand Change = new RoutedUICommand
 (
           "Change",
        "Change",
        typeof(CustomCommands),
        new InputGestureCollection()
        {
             new KeyGesture(Key.F6, ModifierKeys.Alt)
        }
    );
    }
}

XAML:

<Window x:Class="examentest200.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:examentest200"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.New"
                    Executed="NewCommand_Executed"
                    CanExecute="NewCommand_CanExecute"

                                    />
    <CommandBinding Command="local:CustomCommands.Change"
                    Executed="ChangeCommand_Executed"
                    CanExecute="ChangeCommand_CanExecute" />

</Window.CommandBindings>
<Grid>
    <Button x:Name="addButton" Command="ApplicationCommands.New" Content="Add" HorizontalAlignment="Left" Margin="10,24,0,0" VerticalAlignment="Top" Width="75"/>
    <ListBox x:Name="eersteListBox" DisplayMemberPath="Model" SelectedIndex="0" Background="Aquamarine" HorizontalAlignment="Left" Height="100" Margin="110,24,0,0" VerticalAlignment="Top" Width="100"/>
    <ComboBox x:Name="eersteComboBox" DisplayMemberPath="Make" SelectedIndex="0" HorizontalAlignment="Left" Margin="239,28,0,0" VerticalAlignment="Top" Width="100"/>
    <Label x:Name="eersteLabel" DataContext="{Binding Path=SelectedItem, ElementName=eersteListBox, Mode=OneWay}" Content="{Binding Path=Make}" Background="CornflowerBlue" HorizontalAlignment="Left" Margin="376,28,0,0" VerticalAlignment="Top" Width="102"/>
    <Label x:Name="tweedeLabel" DataContext="{Binding Path=SelectedItem, ElementName=eersteListBox, Mode=OneWay}" Content="{Binding Path=Model}" Background="CadetBlue" HorizontalAlignment="Left" Margin="376,70,0,0" VerticalAlignment="Top" Width="102"/>
    <Label x:Name="derdeLabel" DataContext="{Binding Path=SelectedItem, ElementName=eersteListBox, Mode=OneWay}" Content="{Binding Path=Year}" Background="Gold"  HorizontalAlignment="Left" Margin="376,114,0,0" VerticalAlignment="Top" Width="102" Height="28"/>
    <ComboBox x:Name="countryComboBox" SelectedIndex="0" DisplayMemberPath="Land" HorizontalAlignment="Left" Margin="110,187,0,0" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="delButton" Content="Del" HorizontalAlignment="Left" Margin="10,70,0,0" VerticalAlignment="Top" Width="75"/>

    <Button x:Name="changeButton" Command="local:CustomCommands.Change" Content="Change" HorizontalAlignment="Left" Margin="10,114,0,0" VerticalAlignment="Top" Width="75"/>

    <TextBox x:Name="eersteTextBox" Background="LightGray" HorizontalAlignment="Left" Height="23" Margin="376,187,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="102"/>
    <TextBox x:Name="tweedeTextBox" Background="LightBlue" HorizontalAlignment="Left" Height="23" Margin="376,220,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="102"/>
    <TextBox x:Name="derdeTextBox" Background="LightCyan" HorizontalAlignment="Left" Height="23" Margin="376,254,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="102"/>
    <TextBox x:Name="vierdeTextBox" HorizontalAlignment="Left" Height="23" Margin="376,287,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="102"/>
</Grid>

wpf 中的自定义命令绑定

如果收到此错误:

[失败] 找不到文件 'c:''users''bart''documents''visual studio 2015''Projects''examentest200''examentest200''CustomCommand.cs'.

。这可能表示CustomCommands.cs尚未编译。在生成项目之前,XAML 设计器不会看到你的类。