wpf中的自定义命令绑定

时间:2016-11-17 12:19:12

标签: c# wpf xaml

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

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

  

[失败]无法找到文件&c; \ users \ bart \ documents \ visual studio   2015 \项目\ examentest200 \ examentest200 \ CustomCommands.cs'

然而,如果我在资源管理器中检查文件,那就在那里。

提前致谢

" CustomCommands.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>

1 个答案:

答案 0 :(得分:-1)

如果您收到此错误:

  

[失败]无法找到文件'c:\ users \ bart \ documents \ visual studio   2015 \项目\ examentest200 \ examentest200 \ CustomCommands.cs'。

...它可能表明CustomCommands.cs尚未编译。 在项目构建之前,XAML设计师不会看到您的课程。