How to make a control in XAML public in order to be seen in other classes

前端 未结 2 1964
粉色の甜心
粉色の甜心 2020-12-29 20:10

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 w

相关标签:
2条回答
  • 2020-12-29 20:28

    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)
    {
        // ...
    }
    

    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.

    EDIT:

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

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

    Then you set an instance of this class as DataContext:

    • either in code, e.g. window constructor:
    public MyWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
    
    • or in XAML, e.g. 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>
    

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

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

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

    EDIT 2:

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

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

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

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

    <kt:KinectSkeleton ViewModelH="{Binding}" />
    
    0 讨论(0)
  • 2020-12-29 20:43

    Don't make the checkbox visible to the outside, just pass the current state of the checkbox to the function or class. Also consider binding the checkbox value to a class in the DataContext, directly accessing controls can be avoided most of the time in WPF, see also the MVVM pattern.

    0 讨论(0)
提交回复
热议问题