复选框检查事件未在wpf中触发

时间:2015-06-18 18:42:49

标签: c# wpf xaml

hi我的表单(adminentitylist.xaml)文件包含的代码如下

<ResourceDictionary xmlns:local="clr-namespace:iWatch.Administration"  
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 xmlns:my="clr-namespace:iWatch.UILibrary;assembly=UILibrary" 
                 xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
                 xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
                 xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
                 xmlns:cmd="clr-namespace:iWatch.UILibrary;assembly=UILibrary"
                 x:Class="AdminEntityList">

    <ControlTemplate x:Key="AddImgBtnTemplate" TargetType="Button">
        <Grid>
            <Image Name="Normal" Source="/UILibrary;component/Themes/Default/Images/Add.png" Opacity="1.0" Height="18" Width="18"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter TargetName="Normal" Property="Opacity" Value="0.5"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="Normal" Property="Opacity" Value="0.2"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="DelImgBtnTemplate" TargetType="Button">
        <Grid>
            <Image Name="Normal" Source="/UILibrary;component/Themes/Default/Images/Delete.png" Opacity="1.0" Height="18" Width="18"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter TargetName="Normal" Property="Opacity" Value="0.5"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter TargetName="Normal" Property="Opacity" Value="0.2"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

        <Style TargetType="{x:Type local:AdminEntityList}">
        <Setter Property="FontFamily" Value="Calibri"/>
        <Setter Property="dx:ThemeManager.ThemeName" Value="Office2007Black"/>
        <Setter Property="Template" >
            <Setter.Value>

                <ControlTemplate TargetType="{x:Type local:AdminEntityList}">
                    <DockPanel>
                        <DockPanel DockPanel.Dock="Top">
                            <StackPanel Name="StpFilter" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5">
                                <ContentControl x:Name="FilterCriteriaArea" ContentTemplate="{TemplateBinding FilterCriteriaArea}"/>
                            </StackPanel>
                           <StackPanel Name="StpCountry" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5" >
                                <Label  Content="Select Country" Name="lblCountry" Style="{StaticResource lblNormal}" Margin="10,0,5,7" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Visibility="Collapsed"/>
                                <ComboBox Name="cbCountry"  Width="170" Style="{StaticResource ddlNormal}"  SelectedValue="{Binding CountryKey,Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,2,10,0" Visibility="Collapsed"/>
                                <Button Content="Filter" Name="btnFilter" Width="70" Height="23" Margin="0,0,10,0" Visibility="Collapsed"/>                          
                          </StackPanel>
                            <StackPanel Name="actinact" Orientation="Horizontal" HorizontalAlignment="Left" Margin="5">
                                <CheckBox Name="chkactive" Height="18" Content="Active" Checked="chkactive_Checked"></CheckBox>
                                <CheckBox Name="chkinactive" Height="18" Content="In-Active"/>
                            </StackPanel>
                            <StackPanel Name="StpAddDel" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
                                <Button Name="BtnAdd"  Height="20" Width="20" Margin="5,0" Template="{StaticResource AddImgBtnTemplate}" />
                                <Button Name="BtnDel" Height="20" Width="20" Margin="5,0" Template="{StaticResource DelImgBtnTemplate}" />
                            </StackPanel>

                        </DockPanel>
                        <DockPanel Name="AMLDP">
                            <dxg:GridControl Name="genericGrid" AutoPopulateColumns="true"  ShowBorder="False"  
                         DesignTimeDataSourceRowCount="50"  DataSource="{Binding}"
                          Padding="0" Margin="0,0,0,0" IsManipulationEnabled="True" dx:ThemeManager.ThemeName="CPCEGridTheme">
                                <dxg:GridControl.Resources>
                                    <ResourceDictionary x:Key="CPCEGridTheme" Source="/UILibrary;component/Themes/Default/Styles/DataGrid/CPCEGridTheme.xaml" />
                                </dxg:GridControl.Resources>
                                <dxg:GridControl.View>
                                        <dxg:TableView x:Name="tblGeneric" MultiSelectMode="Row" AllowGrouping="True" ClipToBounds="True"  AutoWidth="True" 
                                        ShowGroupPanel="False" ShowHorizontalLines="True" ShowVerticalLines="False" AllowMoveColumnToDropArea="False" AllowDrop="False"                                                                                                  
                                        IsGroupPanelMenuEnabled="False"  AllowEditing="False" NavigationStyle="Cell" Margin="0" 
                                        GroupRowStyle="{StaticResource OddEvenRowStyle}" VirtualizingStackPanel.IsVirtualizing="True" 
                                        VirtualizingStackPanel.VirtualizationMode="Standard" AllowHorizontalScrollingVirtualization="True" 
                                        RowStyle="{StaticResource OddEvenRowStyle}" CellStyle="{StaticResource FocusedCellStyle}">
                                        <dxg:TableView.RowCellMenuCustomizations>
                                            <dxb:BarButtonItem Name="ctmView" IsVisible="False"  Content="View" Command="{x:Static cmd:CustomCommands.View}" />
                                            <dxb:BarButtonItem Name="ctmEdit" IsVisible="True"  Content="Edit" Command="{x:Static cmd:CustomCommands.Edit}" />
                                            <dxb:BarButtonItem Name="ctmMIQ" IsVisible="False"  Content="Manage Interview Questions" Command="{x:Static cmd:CustomCommands.ManageInterviewQuestions}" />
                                            <dxb:BarButtonItem Name="ctmActivate" IsVisible="False" Content="Activate" Command="{x:Static cmd:CustomCommands.Activate}" />
                                            <dxb:BarButtonItem Name="ctmCopyInterviewType" IsVisible="False" Content="Copy Interview Type" Command="{x:Static cmd:CustomCommands.CopyInterviewType}" />
                                            <dxb:BarButtonItem Name="ctmAssignInvGroup" IsVisible="False" Content="Assign Investigative Group" Command="{x:Static cmd:CustomCommands.AssignInvestigativeGroup}" />
                                            <dxb:BarButtonItem Name="ctmCopySelected" IsVisible="False" Content="Copy From Selected" Command="{x:Static cmd:CustomCommands.CopyInterviewTemplate}" />
                                        </dxg:TableView.RowCellMenuCustomizations>
                                    </dxg:TableView>
                                </dxg:GridControl.View>
                                <dxg:GridControl.CommandBindings>
                                    <CommandBinding x:Name="cmdEdit" Command="{x:Static cmd:CustomCommands.Edit}" />
                                    <CommandBinding x:Name="cmdView" Command="{x:Static cmd:CustomCommands.View}" />
                                    <CommandBinding x:Name="cmdMIQ" Command="{x:Static cmd:CustomCommands.ManageInterviewQuestions}" />
                                    <CommandBinding x:Name="cmdActivate" Command="{x:Static cmd:CustomCommands.Activate}" />
                                    <CommandBinding x:Name="cmdCopyInterviewType" Command="{x:Static cmd:CustomCommands.CopyInterviewType}" />
                                    <CommandBinding x:Name="cmdAssignInvestigativeGroup" Command="{x:Static cmd:CustomCommands.AssignInvestigativeGroup}" />
                                    <CommandBinding x:Name="cmdCopySelected" Command="{x:Static cmd:CustomCommands.CopyInterviewTemplate}" />

                                </dxg:GridControl.CommandBindings>
                            </dxg:GridControl>
                        </DockPanel>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

在上面的代码中,我创建了复选框选中的事件Checked="chkactive_Checked"

private void chkactive_Checked(object sender, RoutedEventArgs e)
        {

        }

但事件未触发,我收到错误

  

错误14&#39; AdminEntityList&#39;不包含的定义   &#39; chkactive_Checked&#39;没有扩展方法&#39; chkactive_Checked&#39;   接受类型&#39; AdminEntityList&#39;的第一个参数。可以找到   (您是否缺少using指令或程序集引用?)

请帮我解决问题......

1 个答案:

答案 0 :(得分:0)

这是一个简单的例子:

<Window x:Class="CheckboxCheckedCommand.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <CheckBox Content="Case Sensitive" Command="{Binding checkedCommand}"/>
</Grid>

代码隐藏:

public partial class MainWindow : Window
{
    public ViewModel vm { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        vm = new ViewModel();
        this.DataContext = vm;
    }
}

视图模型:

public class ViewModel
{
    public ICommand checkedCommand { get; set; }

    public ViewModel()
    {
        checkedCommand = new CheckedCommand(this);
    }

    public void CheckedHandler()
    {
        //todo - implement your handler
    }
}

当然还有Checked Unchecked命令:

public class CheckedCommand : ICommand
{
    private ViewModel _vm = null;
    public CheckedCommand(ViewModel _viewModel)
    {
        _vm = _viewModel;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _vm.CheckedHandler();
    }
}

如果您需要更多详细信息,请告诉我,可能是必须从视图发送并由处理程序接收的参数。

我认为这是MVVM的一个简单而正常的流程。你有一些实例/静态方法问题在那里尝试上面的方法,在我看来被清除,并帮助你解耦的东西。

哦,我忘了说,Command="{Binding checkedCommand}",适用于CheckedUnchecked事件。

更新1

这可能是你的代码隐藏:

public partial class MainWindow : Window
{
    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsCheckedProperty =
        DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainWindow), new PropertyMetadata(false, new PropertyChangedCallback(PropertyChanged)));


    private static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        //textbox.ScrollToEnd(); //An object reference is required for the non-static field.

        MainWindow localWindow = (MainWindow)obj;
        Console.WriteLine(localWindow.TestString);
    }

    public string TestString { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        TestString = "test";
        this.DataContext = this;
    }
 }

现在你的CheckBox定义就是这样:

<CheckBox Content="Case Sensitive" IsChecked="{Binding IsChecked}"/>

您基本上使用名称IsChecked定义DependencyProperty。每次检查或取消选中它时,都会调用那里定义的回调方法。并且可以看到关于如何从该静态方法访问实例字段的示例。祝你好运!

相关问题