【发布时间】:2009-03-20 16:13:59
【问题描述】:
如何访问 Sample.xaml.cs 文件中的公共变量,例如 asp.net <%=VariableName%>?
【问题讨论】:
-
This question may also be helpful 因为它是最近的...
标签: c# wpf xaml silverlight
如何访问 Sample.xaml.cs 文件中的公共变量,例如 asp.net <%=VariableName%>?
【问题讨论】:
标签: c# wpf xaml silverlight
有几种方法可以做到这一点。
将您的变量添加为来自代码隐藏的资源:
myWindow.Resources.Add("myResourceKey", myVariable);
然后你可以从 XAML 访问它:
<TextBlock Text="{StaticResource myResourceKey}"/>
如果您必须在解析 XAML 后添加它,您可以使用上面的 DynamicResource 而不是 StaticResource。
使变量成为 XAML 中某些内容的属性。通常这可以通过DataContext:
myWindow.DataContext = myVariable;
或
myWindow.MyProperty = myVariable;
此后,您的 XAML 中的任何内容都可以通过 Binding 访问它:
<TextBlock Text="{Binding Path=PropertyOfMyVariable}"/>
或
<TextBlock Text="{Binding ElementName=myWindow, Path=MyProperty}"/>
【讨论】:
对于绑定,如果DataContext没有被使用,你可以简单的将这个添加到后面代码的构造函数中:
this.DataContext = this;
使用这个,代码中的每个属性都可以被绑定访问:
<TextBlock Text="{Binding PropertyName}"/>
另一种方法是只为 XAML 的根元素命名:
x:Name="root"
由于 XAML 被编译为代码隐藏的部分类,我们可以通过名称访问每个属性:
<TextBlock Text="{Binding ElementName="root" Path=PropertyName}"/>
注意:访问权限仅适用于属性;不去田野。 set; 和 get; 或 {Binding Mode = OneWay} 是必需的。如果使用 OneWay 绑定,则底层数据应实现 INotifyPropertyChanged。
【讨论】:
对于 WPF 中的快速脏窗口,我更喜欢将 Window 的 DataContext 绑定到窗口本身;这都可以在 XAML 中完成。
Window1.xaml
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource self}}"
Title="Window1" Height="300" Width="300">
<StackPanel>
<TextBlock Text="{Binding Path=MyProperty1}" />
<TextBlock Text="{Binding Path=MyProperty2}" />
<Button Content="Set Property Values" Click="Button_Click" />
</StackPanel>
</Window>
Window1.xaml.cs
public partial class Window1 : Window
{
public static readonly DependencyProperty MyProperty2Property =
DependencyProperty.Register("MyProperty2", typeof(string), typeof(Window1), new UIPropertyMetadata(string.Empty));
public static readonly DependencyProperty MyProperty1Property =
DependencyProperty.Register("MyProperty1", typeof(string), typeof(Window1), new UIPropertyMetadata(string.Empty));
public Window1()
{
InitializeComponent();
}
public string MyProperty1
{
get { return (string)GetValue(MyProperty1Property); }
set { SetValue(MyProperty1Property, value); }
}
public string MyProperty2
{
get { return (string)GetValue(MyProperty2Property); }
set { SetValue(MyProperty2Property, value); }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Set MyProperty1 and 2
this.MyProperty1 = "Hello";
this.MyProperty2 = "World";
}
}
在上面的示例中,请注意 Window 上 DataContext 属性中使用的绑定,它表示“将您的数据上下文设置为您自己”。这两个文本块绑定到MyProperty1 和MyProperty2,按钮的事件处理程序将设置这些值,这些值将自动传播到两个文本块的Text 属性,因为属性是依赖属性。
【讨论】:
另外值得注意的是,“绑定”只能在 DependencyObject 的 DependencyProperty 上设置。如果您想在 XAML 中为对象设置非 DependencyProperty(例如普通属性),则必须使用 Robert 在后面的代码中使用资源的第一种方法。
【讨论】:
myWindow.xaml
<Window
...
<TextBlock Text="{ Binding Path=testString }" />
</Window>
myWindow.xaml.cs
public partial class myWindow: Window
{
public string testString { get; set; } = "This is a test string";
public myWindow()
{
DataContext = this;
InitializeComponent();
}
}
重要
Datacontext
testString 必须是public
testString必须是property(有get和set)【讨论】: