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

2

36

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?

Many thanks

EDIT:

Here are 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; }
}

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 => (ViewModel)GetValue(ViewModelProperty);
    set => SetValue(ViewModelProperty, value);
}

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

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
    }

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 bound to the checkbox is_checked value?

Tobolsk answered 11/7, 2012 at 3:34 Comment(1)
see https://mcmap.net/q/427309/-how-to-make-a-label-static only you would use a checkboxConcretize
M
81

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}" />
Mortician answered 11/7, 2012 at 4:45 Comment(16)
ok, here is my exact scenario, I have a project contains and xaml file with its cs file, this xaml has a checkbox lets say cb1, then i have another project having a class x this class has a function y that has to use the value of the check box cb1, and this function is onrender function, so how to do this?Tobolsk
@user1460166 In any case your second project (with class x) needs to reference the first project (with XAML). Then you have 2 options: (1) You could have a CheckBox as a public field in XAML. You then need a reference to an instance of this class in class x so that you can read it in method y. (2) A more clean approach would be a separate class with a boolean property. You set an instance of this class as DataContext in your XAML and bind the property to CheckBox IsChecked property. In class x you have a reference to the same instance of the class and read the property in method y.Mortician
the problem is that the xaml already has a reference to project x, so i can't make a reference of xaml in x cause this will cause a loop of dependency, and how to define the class as datacontext in XAML?Tobolsk
@user1460166 You can set any class as the DataContext. I've updated the answer with additional info. To avoid a loop dependency you need to define the class needed by both projects in the project with class x that is referenced by XAML project or create a new library project referenced by both and define the class there.Mortician
@user1460166 I noticed that in the meantime you posted that as a new question which makes sense since it isn't directly related to this one. You might want to accept one of the answers here it helped you solve the problem.Mortician
i have done the above solution but i don't know how to do the last 2 parts 1- where to write this "DataContext = new ViewModel();"? 2-how to do this "pass the ViewModel instance to your OnRender function. It is stored in the DataContext property of your window."?Tobolsk
@user1460166 (1) If you don't go the XAML route, then add it inside the constructor in your xaml.cs file, just below the InitializeComponent() line. (2) It depends on how your code looks like. One of the options is to set it to a property of your class with OnRender function when you're creating its instance.Mortician
can you please check my other question on this link as i provided code: #11463282Tobolsk
I updated the post with the steps i did, can you please check it? many thanksTobolsk
@user1460166 I moved your attempted solution to your question and extended my original answer.Mortician
1-My need for the button is: i want to do something in KinectSkeleton class when the button is pressed, so how to do this? 2-i don't know where to put this "<kt:KinectSkeleton ViewModelH="{Binding}" />"? there is no kt:KinectSkeleton in the solutionTobolsk
@user1460166 (1) One way would be to add two dependency properties to KinectSkeleton - one for each ViewModel property of interest. In the OnChangedProperty handler of the IsClicked property you can react to the click. (2) I already suggested in the other question how to find where KinectSkeleton is actually used. If it is inside KinectSkeletonViewer as I anticipate, you'll need to add dependency properties to it instead to KinectSkeleton directly.Mortician
(1) can you tell me how in code? (2)I'm trying to browse the code but i can't find where it is, i'm really sorry for my questions but this is my first time to work with wpf and xaml, i did alot of required tasks but the problem now is that i don't know how to make the xaml with all its controls interact with the other classes and how to make the other classes use the values of the xaml controls, thank you very very much for your kind assistance and support i really appreciate itTobolsk
@user1460166 (1) Here you can see how to add a PropertyChangedCallback when registering a DependencyProperty. In OnCurrentReadingChanged method you can add your own logic for handling the change of IsClicked property. (2) Just use the Find in Files feature (Ctrl+Shift+F) in Visual Studio to find all the places where KinectSkeleton is used.Mortician
many thanks for your reply, honestly I'm stuck and I don't know what to do could you apply the solution on the project i sent you in order to understand the root of the problem? I know that I', really disturbing you but if you could please do thatTobolsk
This is a great answer: you both answer the OP's question and then demonstrate the better way to do it.Robotize
T
8

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.

Tegan answered 11/7, 2012 at 3:39 Comment(3)
the problem is that i want to use the value of the checkbox in the onrender function, can you please tell me how to do it by giving a code example?Tobolsk
@user1460166: I cannot give you a code example because i do not your exact object relationships. You should still be able to bind the IsChecked to an object instance that is accessible in the OnRender. The method name sounds like you have a control, then that control could e.g. have a Parameters property which contains an object instance with some bool which you can bind to CheckBox.IsChecked.Tegan
can you check my other question, i added sample of my code: #11463282Tobolsk

© 2022 - 2024 — McMap. All rights reserved.