C# 复选框选中事件未在wpf中触发
hi在我的表单(adminentitylist.xaml)文件中包含的代码如下C# 复选框选中事件未在wpf中触发,c#,wpf,xaml,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.c
<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>
但是事件没有触发,我得到一个错误是
错误14“AdminEntityList”不包含的定义
“chkactive_Checked”和无扩展方法“chkactive_Checked”
无法找到接受类型为“AdminEntityList”的第一个参数
(是否缺少using指令或程序集引用?)
请帮我解决问题…这里有一个简单的例子:
<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 class ViewModel
{
public ICommand checkedCommand { get; set; }
public ViewModel()
{
checkedCommand = new CheckedCommand(this);
}
public void CheckedHandler()
{
//todo - implement your handler
}
}
当然还有选中的命令:
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
这可以是您的代码隐藏:
public partial class MainWindow : Window
{
public ViewModel vm { get; set; }
public MainWindow()
{
InitializeComponent();
vm = new ViewModel();
this.DataContext = vm;
}
}
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 Content="Case Sensitive" IsChecked="{Binding IsChecked}"/>
您基本上使用名称IsChecked来定义DependencyProperty。每次选中或取消选中时,都会调用在其中定义的回调方法。并且可能会看到一个关于如何从该静态方法访问实例字段的示例。祝你好运 下面是一个简单的例子:
<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 class ViewModel
{
public ICommand checkedCommand { get; set; }
public ViewModel()
{
checkedCommand = new CheckedCommand(this);
}
public void CheckedHandler()
{
//todo - implement your handler
}
}
当然还有选中的命令:
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
这可以是您的代码隐藏:
public partial class MainWindow : Window
{
public ViewModel vm { get; set; }
public MainWindow()
{
InitializeComponent();
vm = new ViewModel();
this.DataContext = vm;
}
}
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 Content="Case Sensitive" IsChecked="{Binding IsChecked}"/>
您基本上使用名称IsChecked来定义DependencyProperty。每次选中或取消选中时,都会调用在其中定义的回调方法。并且可能会看到一个关于如何从该静态方法访问实例字段的示例。祝你好运 将该方法设置为受保护或公共。我尝试了使用public和protected,但无效。事件处理程序似乎未连接。它不在正确的代码后面。右键单击xaml并导航到处理程序。或者右键单击并创建处理程序。将该方法设置为受保护的或公共的。我尝试了使用public和protected,但无效。事件处理程序似乎未连接。它不在正确的代码后面。右键单击xaml并导航到处理程序。或者右键单击并创建处理程序。将该方法设置为受保护的或公共的。我尝试了使用public和protected,但无效。事件处理程序似乎未连接。它不在正确的代码后面。右键单击xaml并导航到处理程序。或者右键单击并创建处理程序。在这里,我们没有在MVVM模式中实现,因此请帮助进行正常的处理solution@trinadh尝试在codebehind中定义该命令或定义DependencyProperty并处理回调方法,当值改变时。这里我们没有在MVVM模式中实现,所以请帮助进行正常操作solution@trinadh尝试在codebehind中定义该命令或定义DependencyProperty并处理回调方法,当值改变时。这里我们没有在MVVM模式中实现,所以请帮助进行正常操作solution@trinadh当值更改时,尝试在codebehind中定义该命令或定义DependencyProperty并处理回调方法。