如何绑定类中的数据

时间:2019-01-20 10:35:59

标签: c# wpf xaml binding

我有这个代码示例

Class1.cs:

namespace WpfApp1
{
    public class Class1
    {
        public string ForButton { get; set; } = "I'm button";
        public string ForTextBlock { get; set; } = "Ï'm TextBlock";
    }
}

MainWindow.xaml.cs:

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Class1 test = new Class1();

        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

和MainWindow.xaml:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">

    <StackPanel>
        <Button Height="30"/>
        <TextBlock Height="30"/>
    </StackPanel>
</Window>

我想知道如何将字符串ForButtonForTextBlock绑定为 Button Button Text TextBox 的适用性。

1 个答案:

答案 0 :(得分:0)

本文回答了您的问题,甚至更多:D。

Article link