XAML从字符串绑定到文本框不能正常工作 [英] XAML binding from a string to TextBox not working

查看:107
本文介绍了XAML从字符串绑定到文本框不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是谁的有理解为什么他的理解XAML绑定的第一次尝试是不工作的问题一个C#新手。我下面的Microsoft的数据绑定概述

I'm a C# newbie who's having problems understanding why his first attempt at understanding XAML bindings isn't working. I'm following Microsoft's Data Binding Overview.

我有一个单一的文本框,这将最终成为一个状态窗口。现在我只是希望能够任意的文本字符串写进去。我也有我测试的命令模式的一个实例。我的命令包括添加一个随机数到累加器,并打印出结果的状态视图

I have a single TextBox which will eventually serve as a status window. For now I just want to be able to write arbitrary text strings into it. I also have an instance of the command pattern that I'm testing. My command involves adding a random number to an accumulator and printing out the result to the status view.

下面是我的主应用程序窗口XAML:

Here's my XAML for the main application window:

<Window x:Class="Experiment.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:Experiment"
        Title="Test" Height="500" Width="700" Name="Test" Closing="Test_Closing" DataContext="{Binding ElementName=statusText}" xmlns:my="clr-namespace:Experiment">
    <Window.Resources>
        <c:StatusViewText x:Key="statusViewText" />
    </Window.Resources>
    <DockPanel HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto">
        <!-- Elements deleted for brevity. -->
        <TextBox Margin="5,5,5,5" Name="statusText" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" AcceptsReturn="True" AcceptsTab="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" FontFamily="Courier New" FontSize="12"
                 Text="{Binding Source={StaticResource statusViewText}, Path=statusTextString, Mode=OneWay}"/>
    </DockPanel>
</Window>

和代表我的数据的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Experiment {
    public class StatusViewText {
        public StringBuilder statusText { get; set; }
        public String statusTextString { get; set; }

        public StatusViewText() {
            statusText = new StringBuilder();
        }

        public void Append(string s) {
            if (s != null) {
                statusText.Append(s);
            }

            statusTextString = statusText.ToString();
        }

        public void AppendLine(string s) {
            if (s != null) {
                statusText.AppendLine(s);
            }

            statusTextString = statusText.ToString();
        }
    }
}



最后,我将使用从这里StringBuilder的适当的转换器,但是我想探索的复杂性之前得到的原则了。

Eventually I'll use a proper converter here from StringBuilder, but I wanted to get the principle down before exploring that complexity.

如果我的理解是正确的(显然它不是),这一切都应该工作。绑定目标是在TextBox,目标属性是Text属性。绑定源是StatusViewText类的statusTextString属性。然而,当我运行命令(和调试,看看StatusViewText.statusTextString正在更新),文本框不更新。

If my understanding were correct (and obviously it isn't), this all SHOULD work. The binding target is the TextBox, target property is the Text property. The binding source is the StatusViewText class's statusTextString property. Yet when I run the command (and debug and see StatusViewText.statusTextString being updated), the TextBox doesn't update.

我想我可能需要明确添加结合我自己,所以我试图在主窗口的构造后的InitializeComponent添加此()

I thought that I may need to explicitly add the binding myself, so I tried adding this after InitializeComponent() in the main window constructor:

        statusViewText = new StatusViewText();
        Binding statusViewTextBinding = new Binding("statusTextString");
        statusViewTextBinding.Source = statusViewText;
        statusText.SetBinding(TextBlock.TextProperty, statusViewTextBinding);



但也不能工作。

but that didn't work either.

我是否需要火的PropertyChanged事件?我想绑定框架的整个观点是,事件被解雇,消耗的场景,自动的背后; 。但也许我错了,有太多

Do I need to fire PropertyChanged events? I thought the entire point of the binding framework is that events are fired and consumed behind the scenes, automagically; but maybe I'm wrong there too.

没有明显的错误在这使我相信,我的绑定语法是正确的输出窗口上来;我只是缺少的东西。

No obvious errors are coming up in the Output window which leads me to believe that my binding syntax is correct; I'm just missing something else.

HALP!

修改13:14 EDT 感谢 mben

好吧,我这样做。增加了以下内容:

Ok, I did that. Added the following:

public class StatusViewText : INotifyPropertyChanged {
    public void Append(string s) {
        if (s != null) {
            statusText.Append(s);
        }

        statusTextString = statusText.ToString();
        NotifyPropertyChanged("statusTextString");
    }

    public void AppendLine(string s) {
        if (s != null) {
            statusText.AppendLine(s);
        }

        statusTextString = statusText.ToString();
        NotifyPropertyChanged("statusTextString");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}



我调试,以验证它正想通过这个代码路径,它是。但仍没有运气。任何其他的想法?

I debugged to verify that it was going through this code path, and it is. But still no luck. Any other thoughts?

推荐答案

您还需要实现您StatusViewText的INotifyPropertyChanged的。
结合系统是不会持续检查值,则需要在情况发生变化,通知。

You still need to implement the INotifyPropertyChanged on your StatusViewText. The binding system is not going to check the values continuously, you need to notify it when things change.

这篇关于XAML从字符串绑定到文本框不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆