如何在XAML中公开控件以便在其他类中看到 [英] How to make a control in XAML public in order to be seen in other classes

查看:64
本文介绍了如何在XAML中公开控件以便在其他类中看到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf应用程序中工作,我在XAML中创建了一个复选框,然后我的代码在一个类中调用了一个函数,并且在此函数中存在一个if条件,其条件是检查该复选框是否被选中,但该复选框被选中.在本课程中没有看到,所以该怎么做?

I'm working in wpf application i made a checkbox in the XAML, then my code calls a function in a class and in this function there is an if condition where its checking on whether the checkbox is checked or not but the checkbox is not seen in this class, so how to do this?

非常感谢

这是我执行的步骤: 我在KinectSkeleton的同一项目下创建了ViewModel类,如下所示: ViewModel类:

Here is the steps I did: i created the ViewModel class under the same project of KinectSkeleton as shown: ViewModel class:

public class ViewModel
{
    public bool IsChecked { get; set; }
    public bool is_clicked { get; set; }
}

在KinectSkeleton中,我定义了一个属性,如下所示:

and in the KinectSkeleton I defined a property as shown:

public static readonly DependencyProperty ViewModelProperty =
           DependencyProperty.Register("ViewModelH", typeof(ViewModel), typeof(KinectSkeleton), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));


public ViewModel ViewModelH
{
    get
    {
        return (ViewModel)GetValue(ViewModelProperty);
    }
    set
    {
        SetValue(ViewModelProperty, value);
    }
}

以及KinectWindow.xaml中的复选框和按钮的代码是:

and the code of the checkbox and button in the KinectWindow.xaml is :

<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中,我要检查我编写的复选框的值:

and in the KinectSkeleton where i want to check the value of the checkbox i write:

    if (this.ViewModelH.IsChecked == false)
    // if(f.is_chekced==false)
    {
        // do something
    }

现在我想知道在复选框的is_checked事件和按钮的is_clicked中写什么?在上述步骤中还缺少任何内容,因为我觉得到目前为止,kinect骨架属性尚未绑定到复选框is_checked值?

now i want to know what to write in the is_checked event of the checkbox and is_clicked of the button? also is there anything missing in my above steps as i feel that till now the kinect skeleton property is not binded to the checkbox is_checked value?

推荐答案

使用以下XML,您可以将控件定义为类的公共字段,以便能够从其他类访问它:

Using the following XML you can define a control as a public field on the class to be able to access it from other classes:

<CheckBox x:Name="myCheckBox" x:FieldModifier="public" />

现在您可以直接在代码中访问该字段:

Now you can access the field directly in code:

if (win.myCheckBox.IsChecked.Value)
{
    // ...
}

不过,我同意H.B.的观点,即使用MVVM模式是一种更好的方法.您代码的其他部分不应该知道您的UI或直接访问它.

I agree with H.B., though, that using the MVVM pattern is a better way to do it. Other parts of your code shouldn't be aware of your UI or directly access it.

使用MVVM方法,您首先应该定义视图模型类:

With the MVVM approach you should first define your view model class:

public class ViewModel
{
    public bool IsChecked { get; set; }
}

然后将此类的实例设置为DataContext:

Then you set an instance of this class as DataContext:

  • 无论是在代码中,例如窗口构造器:
public MyWindow()
{
    InitializeComponent();
    DataContext = new ViewModel();
}

  • 或在XAML中,例如App.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绑定到ViewModel中的属性:

    Now you can bind your CheckBox to a property in ViewModel:

    <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" />
    

    剩下的就是将ViewModel实例传递给您的OnRender函数.它存储在窗口的DataContext属性中.

    All that's left is to pass the ViewModel instance to your OnRender function. It is stored in the DataContext property of your window.

    顺便说一句:在接受答案之前,您确实应该问过这个问题.

    BTW: You really should have asked that before you accepted the answer.

    我不确定您要使用is_clicked属性尝试什么.要在单击按钮时设置此标志,您需要Command:

    I'm not sure what you are trying to attempt with the is_clicked property. To set this flag when the button is clicked, you need a 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;
        }
    }
    

    您将此命令的实例添加到视图模型:

    You add an instance of this command to your view model:

    public class ViewModel
    {
        public bool IsChecked { get; set; }
        public bool IsClicked { get; set; }
        public ICommand CalibrateCommand { get; set; }
    
        public ViewModel()
        {
            CalibrateCommand = new CalibrateCommand(this);
        }
    }
    

    您将其绑定到按钮,如下所示:

    You bind it to the button like this:

    <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}" />
    

    您不需要处理CheckBoxButton的任何事件,所有事情都由绑定处理.

    You don't need to handle any events of the CheckBox and the Button, everything is handled by the binding.

    如果将依赖项属性添加到KinectSkeleton,则应将其绑定到视图模型:

    If you added a dependency property to KinectSkeleton you should bind it to the view model:

    <kt:KinectSkeleton ViewModelH="{Binding}" />
    

    这篇关于如何在XAML中公开控件以便在其他类中看到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆