在XAML中访问变量后面的代码

时间:2009-03-20 16:13:59

标签: c# wpf xaml silverlight

如何访问 Sample.xaml.cs 文件中的公共变量,如asp.net <%=VariableName%>

5 个答案:

答案 0 :(得分:62)

有几种方法可以做到这一点。

  • 将您的变量添加为codebehind:

    中的资源
    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}"/>
    

答案 1 :(得分:24)

对于绑定,如果未使用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

答案 2 :(得分:9)

对于WPF中的快速脏Windows,我更喜欢将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属性中使用的绑定,这表示“将数据上下文设置为自己”。两个文本块绑定到MyProperty1MyProperty2,按钮的事件处理程序将设置这些值,这些值将自动传播到两个TextBlock的Text属性,因为属性是依赖属性。

答案 3 :(得分:3)

值得注意的是,'Binding'只能在DependencyObject的DependencyProperty上设置。如果要在XAML中的对象上设置非DependencyProperty(例如,普通属性),则必须使用Robert的第一种方法来使用后面代码中的资源。

答案 4 :(得分:1)

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(有getset