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?
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:
Now you can access the field directly in code:
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:
Then you set an instance of this class as
DataContext:Now you can bind your CheckBox to a property in ViewModel:
All that’s left is to pass the
ViewModelinstance to yourOnRenderfunction. It is stored in theDataContextproperty 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_clickedproperty. To set this flag when the button is clicked, you need aCommand:You add an instance of this command to your view model:
You bind it to the button like this:
You don’t need to handle any events of the
CheckBoxand theButton, everything is handled by the binding.If you added a dependency property to
KinectSkeletonyou should bind it to the view model: