Binding UpdateSourceTrigger=Explicit,在程序启动时更新源

发布于 2024-08-06 03:48:05 字数 1444 浏览 10 评论 0原文

我有以下代码:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <TextBox Text="{Binding Path=Name, 
                            Mode=OneWayToSource, 
                            UpdateSourceTrigger=Explicit, 
                            FallbackValue=default text}" 
             KeyUp="TextBox_KeyUp" 
             x:Name="textBox1"/>
</Grid>

    public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void TextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            BindingExpression exp = this.textBox1.GetBindingExpression(TextBox.TextProperty);
            exp.UpdateSource();
        }
    }
}



    public class ViewModel
{
    public string Name
    {
        set
        {
            Debug.WriteLine("setting name: " + value);
        }
    }
}



    public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        Window1 window = new Window1();
        window.DataContext = new ViewModel();
        window.Show();
    }
}

我只想在文本框中按下“Enter”键时更新源。这很好用。但是,绑定会在程序启动时更新源。我怎样才能避免这种情况?我错过了什么吗?

I have following code:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <TextBox Text="{Binding Path=Name, 
                            Mode=OneWayToSource, 
                            UpdateSourceTrigger=Explicit, 
                            FallbackValue=default text}" 
             KeyUp="TextBox_KeyUp" 
             x:Name="textBox1"/>
</Grid>

    public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void TextBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            BindingExpression exp = this.textBox1.GetBindingExpression(TextBox.TextProperty);
            exp.UpdateSource();
        }
    }
}



    public class ViewModel
{
    public string Name
    {
        set
        {
            Debug.WriteLine("setting name: " + value);
        }
    }
}



    public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        Window1 window = new Window1();
        window.DataContext = new ViewModel();
        window.Show();
    }
}

I want to update source only when "Enter" key is pressed in textbox. This works fine. However binding updates source at program startup. How can I avoid this? Am I missing something?

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

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

发布评论

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

评论(2

焚却相思 2024-08-13 03:48:05

问题是,DataBinding 在调用 Show 时得到解决(以及在 InitializeComponent 上,但这对您来说并不重要,因为此时您的 DataContext 尚未设置)。我认为您无法阻止这种情况,但我有一个解决方法的想法:

在调用 Show() 之前不要设置 DataContext。您可以像这样实现这一点(例如):

public partial class Window1 : Window
{
    public Window1(object dataContext)
    {
        InitializeComponent();

        this.Loaded += (sender, e) =>
        {
            DataContext = dataContext;
        };
    }
}

并且:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    Window1 window = new Window1(new ViewModel());
    window.Show();
}

The problem is, that DataBinding is resolved on the call of Show (and on InitializeComponent, but that is not important for you, because at that point your DataContext is not set yet). I don't think you can prevent that, but I have an idea for a workaround:

Do not set the DataContext before you call Show(). You can achieve this (for example) like this:

public partial class Window1 : Window
{
    public Window1(object dataContext)
    {
        InitializeComponent();

        this.Loaded += (sender, e) =>
        {
            DataContext = dataContext;
        };
    }
}

and:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    Window1 window = new Window1(new ViewModel());
    window.Show();
}
夏见 2024-08-13 03:48:05

将您的绑定模式更改为默认

<TextBox Text="{Binding Path=Name, 
                    Mode=Default, 
                    UpdateSourceTrigger=Explicit, 
                    FallbackValue=default text}" 
        KeyUp="TextBox_KeyUp" 
        x:Name="textBox1"/>

Change your Binding Mode to Default

<TextBox Text="{Binding Path=Name, 
                    Mode=Default, 
                    UpdateSourceTrigger=Explicit, 
                    FallbackValue=default text}" 
        KeyUp="TextBox_KeyUp" 
        x:Name="textBox1"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文