我在 wpf 应用程序中工作,我在 XAML 中创建了一个复选框,然后我的代码调用了一个类中的函数,在这个函数中有一个 if 条件,它检查复选框是否被选中,但是复选框在这个类中没有看到,那么如何做到这一点?
非常感谢
编辑:
以下是我做的步骤: 我在 KinectSkeleton 的同一个项目下创建了 ViewModel 类,如下所示: View 模型类:
public class ViewModel
{
public bool IsChecked { get; set; }
public bool is_clicked { get; set; }
}
我在 KinectSkeleton 中定义了一个属性,如下所示:
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register("ViewModelH", typeof(ViewModel), typeof(KinectSkeleton), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public ViewModel ViewModelH
{
get => (ViewModel)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
KinectWindow.xaml中复选框和按钮的代码是:
<Button Content="Calibrate" Height="24" x:Name="Calibrate" x:FieldModifier="public" Width="90" Click="Calibrate_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Left" DockPanel.Dock="Left" Panel.ZIndex="0" Padding="0" VerticalAlignment="Center" />
<CheckBox IsChecked="{Binding Mode=TwoWay, Path=IsChecked}" Content="AngleDifference" Height="22" x:Name="AngleDifference" x:FieldModifier="public" Width="117" Checked="AngleDifference_Checked" Unchecked="AngleDifference_Unchecked" HorizontalAlignment="Left" VerticalAlignment="Center" Panel.ZIndex="1" HorizontalContentAlignment="Left" />
在我想检查复选框值的 KinectSkeleton 中,我写了:
if (this.ViewModelH.IsChecked == false)
// if(f.is_chekced == false)
{
// do something
}
现在我想知道checkbox的is_checked事件和button的is_clicked事件要写什么?另外,我觉得到目前为止 Kinect 骨架属性没有绑定(bind)到复选框 is_checked 值,我的上述步骤是否遗漏了什么?
最佳答案
使用以下 XML,您可以将控件定义为类的公共(public)字段,以便能够从其他类访问它:
<CheckBox x:Name="myCheckBox" x:FieldModifier="public" />
现在您可以直接在代码中访问该字段:
if (win.myCheckBox.IsChecked.Value)
{
// ...
}
不过,我同意 H.B. 的观点,即使用 MVVM 模式是更好的方法。您的代码的其他部分不应该知道您的 UI 或直接访问它。
编辑:
使用 MVVM 方法,您应该首先定义 View 模型类:
public class ViewModel
{
public bool IsChecked { get; set; }
}
然后将此类的实例设置为 DataContext
:
- 在代码中,例如窗口构造函数:
public MyWindow() { InitializeComponent(); DataContext = new ViewModel(); }
- 或在 XAML 中,例如应用程序.xaml:
<Application x:Class="WpfApplication2.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:WpfApplication2" StartupUri="MainWindow.xaml"> <Application.Resources> <vm:ViewModel x:Key="ViewModel" /> </Application.Resources> </Application>
现在您可以将 CheckBox 绑定(bind)到 ViewModel 中的属性:
<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" />
剩下的就是将 ViewModel
实例传递给您的 OnRender
函数。它存储在窗口的 DataContext
属性中。
编辑 2:
顺便说一句:你真的应该在接受答案之前问这个问题。
我不确定您要尝试使用 is_clicked
属性做什么。要在单击按钮时设置此标志,您需要一个 Command
:
public class CalibrateCommand : ICommand
{
private ViewModel viewModel;
public CalibrateCommand(ViewModel viewModel)
{
this.viewModel = viewModel;
}
public void Execute(object parameter)
{
viewModel.IsClicked = true;
}
public bool CanExecute()
{
return true;
}
}
您将此命令的一个实例添加到您的 View 模型中:
public class ViewModel
{
public bool IsChecked { get; set; }
public bool IsClicked { get; set; }
public ICommand CalibrateCommand { get; set; }
public ViewModel()
{
CalibrateCommand = new CalibrateCommand(this);
}
}
你可以像这样将它绑定(bind)到按钮上:
<Button Content="Calibrate" Height="24" x:Name="Calibrate" x:FieldModifier="public" Width="90" Click="Calibrate_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Left" DockPanel.Dock="Left" Panel.ZIndex="0" Padding="0" VerticalAlignment="Center" Command="{Binding CalibrateCommand}" />
您不需要处理 CheckBox
和 Button
的任何事件,一切都由绑定(bind)处理。
如果您向 KinectSkeleton
添加了一个依赖属性,您应该将它绑定(bind)到 View 模型:
<kt:KinectSkeleton ViewModelH="{Binding}" />
关于c# - 如何在 XAML 中公开控件以便在其他类中看到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425153/