【问题标题】:Access codebehind variable in XAML访问 XAML 中的代码隐藏变量
【发布时间】:2009-03-20 16:13:59
【问题描述】:

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

【问题讨论】:

标签: c# wpf xaml silverlight


【解决方案1】:

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

  • 将您的变量添加为来自代码隐藏的资源:

    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}"/>
    

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢!这个答案帮助了我。我无法使用 DataContext 方法,因为我正在创建一个用户控件,如果我在我的控件中设置数据上下文,那么当我在应用程序中使用它时,我的控件将无法获取其父级的数据上下文。直接绑定到根给了我我正在寻找的相同结果,并且我的控件的数据上下文保持不变。
    • 感谢“根”的建议。尽管视觉绑定设计器中未列出代码隐藏的属性,但它仍然有效,这有点令人困惑。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      另外值得注意的是,“绑定”只能在 DependencyObject 的 DependencyProperty 上设置。如果您想在 XAML 中为对象设置非 DependencyProperty(例如普通属性),则必须使用 Robert 在后面的代码中使用资源的第一种方法。

      【讨论】:

        【解决方案5】:

        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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-07-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-26
          相关资源
          最近更新 更多