XAML代码中的C#变量
我有一个XAML表单的部分类代码。下面是隐藏的代码:XAML代码中的C#变量,c#,xaml,code-behind,C#,Xaml,Code Behind,我有一个XAML表单的部分类代码。下面是隐藏的代码: partial class GalleryDictionary { private void GalleryItemLabelPropertyChanged(object sender, RoutedEventArgs e) { var mainWindow = Application.Current.MainWindow as MainWindow; if (mainWindow == nul
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();
}
}
private void DS_Loaded(object sender, RoutedEventArgs e)
{
ToggleButton tg = sender as ToggleButton;
if (//WhateverYourPathToTheVariable//.DriveOnRight == "True")
{
tg.Content = "DS";
}
else
{
tg.Content = "PS";
}
}
这是名称空间声明之后的整个DOC.XAML.CS文件
在我的XAML文件中,我需要从这个XAML.CS文件中访问一个变量,我想调用DriveOnRight。这是我一直在奋斗的路线:
<ToggleButton Grid.Row="1" Grid.Column="2" Width="26" Height="26" IsChecked="True" Click="GalleryItemLabelPropertyChanged" CommandParameter="2" Name="PS" Content="{Binding DriveOnRight}"></ToggleButton>
XAML文件读取的地方是….Content=“{Binding DriveOnRight}”,我需要从CS文件访问变量并在那里显示它
我该怎么做
任何帮助都将不胜感激。这里有一种方法:
public class GalleryDictionaryViewmodel: INotifyPropertyChanged
{
private string _DriveOnRight;
public string DriveOnRight
{
get{ return _DriveOnRight;}
set{ _DriveOnRight = value;}
}
}
在代码隐藏文件(DOC.XAML.CS)中:
在Xaml文件中:
<ToggleButton Grid.Row="1" Grid.Column="2" Width="26" Height="26" IsChecked="True" Click="GalleryItemLabelPropertyChanged" CommandParameter="2" Name="PS" Content="{Binding DriveOnRight}"></ToggleButton>
希望这能对您有所帮助。鉴于这在您的代码背后,为了与您的方法保持一致,您可以使用依赖属性作为您的值
public string DriveOnRight
{
get { return (string)GetValue(DriveOnRightProperty); }
set { SetValue(DriveOnRightProperty, value); }
}
public static readonly DependencyProperty DriveOnRightProperty =
DependencyProperty.Register("DriveOnRight", typeof(string), typeof(GalleryDictionary));
尽管我建议Krish的设计更适合使用ViewModel。您可以在代码中指定一个值,该值与按钮或切换按钮或任何使用它的对象的加载功能相关 在函数上方的某个级别中设置值,使代码如下所示
public static string DriveOnRight = "True";
//"True" could be a condition or function or whatever.
然后,在代码隐藏中可能是这样的:
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();
}
}
private void DS_Loaded(object sender, RoutedEventArgs e)
{
ToggleButton tg = sender as ToggleButton;
if (//WhateverYourPathToTheVariable//.DriveOnRight == "True")
{
tg.Content = "DS";
}
else
{
tg.Content = "PS";
}
}
然后…在XAML代码中,您有:
<ToggleButton Loaded="DS_Loaded" Grid.Row="1" Grid.Column="0" Width="26" Height="26" Name="DS" Content="DS"></ToggleButton>