首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复选框选中的事件在wpf中不触发。

复选框选中的事件在wpf中不触发。
EN

Stack Overflow用户
提问于 2015-06-18 18:42:49
回答 1查看 2.7K关注 0票数 1

嗨,在我的表单(adminentitylist.xaml)文件中包含下面的代码

代码语言:javascript
复制
<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"

代码语言:javascript
复制
private void chkactive_Checked(object sender, RoutedEventArgs e)
        {

        }

但是事件没有触发,我得到的错误是

错误14 'AdminEntityList‘不包含'chkactive_Checked’的定义,也找不到接受'AdminEntityList‘类型的第一个参数的扩展方法'chkactive_Checked’(您缺少使用指令还是程序集引用?)

请帮我解决这个问题..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-18 19:14:59

下面是一个简单的例子:

代码语言:javascript
复制
<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>

代码背后:

代码语言:javascript
复制
public partial class MainWindow : Window
{
    public ViewModel vm { get; set; }

    public MainWindow()
    {
        InitializeComponent();

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

ViewModel:

代码语言:javascript
复制
public class ViewModel
{
    public ICommand checkedCommand { get; set; }

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

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

当然,选中的未检查命令:

代码语言:javascript
复制
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}"既适用于Checked事件,也适用于Unchecked事件。

更新1

这可以是您的代码背后:

代码语言:javascript
复制
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的定义是:

代码语言:javascript
复制
<CheckBox Content="Case Sensitive" IsChecked="{Binding IsChecked}"/>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30923283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档