如何在WPF中绑定单个对象实例?

发布于 2024-10-19 12:25:58 字数 1440 浏览 1 评论 0原文

我是一个WPF新人,我已经找了两天了,没有运气。我有一个 WPF 窗口,其中包含多个文本框控件和一个具有某些属性的对象。该对象在其构造函数中传递到我的 WPF 窗口的代码隐藏:

public partial class SettingsDialog : Window
{
    public SettingsObject AppSettings
    {
        get;
        set;
    }

    public SettingsDialog(SettingsObject settings)
    {
        this.AppSettings = settings;
        InitializeComponent();
    }
}

SettingsObject 看起来像这样(为了清晰起见,进行了简化):

public class SettingsObject
{
    public string Setting1 { get; set; }
    public string Setting2 { get; set; }
    public string Setting3 { get; set; }

    public SettingsObject()
    {
        this.Setting1 = "ABC";
        this.Setting2 = "DEF";
        this.Setting3 = "GHI";
    }
}

我的 WPF 窗口(简化了):

<Window x:Class="MyProgram.SettingsDialog" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding Source=AppSettings}">
    <Grid>
        <TextBox Name="Setting1Textbox" Text="{Binding Path=Setting1}"></TextBox>
        <TextBox Name="Setting2Textbox" Text="{Binding Path=Setting2}"></TextBox>
        <TextBox Name="Setting3Textbox" Text="{Binding Path=Setting3}"></TextBox>
    </Grid>
</Window>

如何实现双向绑定在这种情况下?我已经尝试过您在上面看到的内容(以及更多),但没有任何效果!

I am a WPF newcomer, and I've been searching for two days with no luck. I have a WPF window that has several text box controls, and a single object with some properties. This object is passed to the codebehind of my WPF window in it's constructor:

public partial class SettingsDialog : Window
{
    public SettingsObject AppSettings
    {
        get;
        set;
    }

    public SettingsDialog(SettingsObject settings)
    {
        this.AppSettings = settings;
        InitializeComponent();
    }
}

The SettingsObject looks something like this (simplified for clarity):

public class SettingsObject
{
    public string Setting1 { get; set; }
    public string Setting2 { get; set; }
    public string Setting3 { get; set; }

    public SettingsObject()
    {
        this.Setting1 = "ABC";
        this.Setting2 = "DEF";
        this.Setting3 = "GHI";
    }
}

And my WPF window (simplified):

<Window x:Class="MyProgram.SettingsDialog" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding Source=AppSettings}">
    <Grid>
        <TextBox Name="Setting1Textbox" Text="{Binding Path=Setting1}"></TextBox>
        <TextBox Name="Setting2Textbox" Text="{Binding Path=Setting2}"></TextBox>
        <TextBox Name="Setting3Textbox" Text="{Binding Path=Setting3}"></TextBox>
    </Grid>
</Window>

How do you acheive two-way binding in this situation? I've tried what you see above (and so much more) but nothing works!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2024-10-26 12:25:58

您是否将窗口的 DataContext 属性设置为 AppSettings 实例?

public SettingsDialog(SettingsObject settings)
{

    InitializeComponent();

    //While this line should work above InitializeComponent,
    // it's a good idea to put your code afterwards.
    this.AppSettings = settings;

    //This hooks up the windows data source to your object.
    this.DataContext = settings;
}

Have you set the DataContext property of the window to your instance of AppSettings?

public SettingsDialog(SettingsObject settings)
{

    InitializeComponent();

    //While this line should work above InitializeComponent,
    // it's a good idea to put your code afterwards.
    this.AppSettings = settings;

    //This hooks up the windows data source to your object.
    this.DataContext = settings;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文