Reputation: 21
I have a partial class code behind for a XAML form. Here is the code behind:
partial class GalleryDictionary
{
private void GalleryItemLabelPropertyChanged(object sender, RoutedEventArgs e)
{
var mainWindow = Application.Current.MainWindow as MainWindow;
if (mainWindow == null)
return;
var tb = sender as TextBox;
if (tb == null)
return;
var sip = tb.DataContext as StereoImagePair;
if (sip == null)
return;
sip.Label = tb.Text;
mainWindow.Db.SubmitChanges();
}
}
That is the entire DOC.XAML.CS file after the namespace declaration.
In my XAML file I need to access a variable from this XAML.CS file that I'd like to call DriveOnRight. Here is the line I've been fighting:
<ToggleButton Grid.Row="1" Grid.Column="2" Width="26" Height="26" IsChecked="True" Click="GalleryItemLabelPropertyChanged" CommandParameter="2" Name="PS" Content="{Binding DriveOnRight}"></ToggleButton>
Where the XAML file reads....Content="{Binding DriveOnRight}" is where I need to access the variable from the CS file and display it there.
How do I do that?
Any help sure will be appreciated.
Upvotes: 2
Views: 5191
Reputation: 1
You could assign a value in the code behind that is associated to the LOADED function of the button or toggle button or whatever object uses it.
In a level above your function to set the value, make code like this.
public static string DriveOnRight = "True";
//"True" could be a condition or function or whatever.
Then, it might look like this in the code behind:
private void DS_Loaded(object sender, RoutedEventArgs e)
{
ToggleButton tg = sender as ToggleButton;
if (//WhateverYourPathToTheVariable//.DriveOnRight == "True")
{
tg.Content = "DS";
}
else
{
tg.Content = "PS";
}
}
And then...in the XAML code, you have this:
<ToggleButton Loaded="DS_Loaded" Grid.Row="1" Grid.Column="0" Width="26" Height="26" Name="DS" Content="DS"></ToggleButton>
Upvotes: 0
Reputation: 376
Here is one of the way:
public class GalleryDictionaryViewmodel: INotifyPropertyChanged
{
private string _DriveOnRight;
public string DriveOnRight
{
get{ return _DriveOnRight;}
set{ _DriveOnRight = value;}
}
}
In your CodeBehind File (DOC.XAML.CS):
partial class GalleryDictionary
{
public GalleryDictionaryViewModel GDViewModel;
public GalleryDictionary()
{
GDViewMOdel = new GDViewModel();
GDViewModel.DriveOnRight = "";
this.DataContext = GDViewModel;
}
}
In Your Xaml file:
<ToggleButton Grid.Row="1" Grid.Column="2" Width="26" Height="26" IsChecked="True" Click="GalleryItemLabelPropertyChanged" CommandParameter="2" Name="PS" Content="{Binding DriveOnRight}"></ToggleButton>
Hope this will help you.
Upvotes: 0
Reputation: 6201
Seeing as this is in your code behind, to remain consistent with your approach, you could use a dependency property for your value.
public string DriveOnRight
{
get { return (string)GetValue(DriveOnRightProperty); }
set { SetValue(DriveOnRightProperty, value); }
}
public static readonly DependencyProperty DriveOnRightProperty =
DependencyProperty.Register("DriveOnRight", typeof(string), typeof(GalleryDictionary));
Although I would suggest Krish's design is better with the use of a ViewModel.
Upvotes: 1