簡體   English   中英

訪問 XAML 中的代碼隱藏變量

[英]Access codebehind variable in XAML

如何訪問Sample.xaml.cs文件中的公共變量,例如 asp.net <%=VariableName%>

有幾種方法可以做到這一點。

  • 將您的變量添加為代碼隱藏中的資源:

     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 被編譯為代碼隱藏的部分 class,我們可以通過名稱訪問每個屬性:

<TextBlock Text="{Binding ElementName="root" Path=PropertyName}"/>

注意:訪問僅適用於屬性; 不去田野。 set; get; {Binding Mode = OneWay}是必需的。 如果使用 OneWay 綁定,則底層數據應實現INotifyPropertyChanged

對於 WPF 中的快速和骯臟的 Windows,我更喜歡將 Window 的 DataContext 綁定到 Z05ZB8C74CBD96FBAFDE4 本身; 這都可以在 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 ,按鈕的事件處理程序將設置這些值,這些值將自動傳播到兩個 TextBlocks 的Text屬性,因為屬性是依賴屬性。

還值得注意的是,“綁定”只能在 DependencyObject 的 DependencyProperty 上設置。 如果您想在 XAML 中的 object 上設置非 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 (有一個getset

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM