Textbox.Text输入未绑定到Property

时间:2012-07-30 22:19:02

标签: c# wpf data-binding textbox

我有一个已创建的对象,并希望通过模式OneWayToSource显式绑定到该对象的属性。然而,这种约束根本不起作用。当程序初始化时,它在文本框周围也有一个红色边框,当我单击按钮时我只想要输入验证。我的最后一个问题是将源代码嵌入到元素本身,但没有这样的运气。这就是我所拥有的:

<StackPanel.Resources>
    <my:HoursWorked x:Key="hwViewSource" /> 
</StackPanel.Resources>

<TextBox Style="{StaticResource textBoundStyle}" Name="adminTimeTxtBox">
    <Binding Source="{StaticResource hwViewSource}" Path="Hours" UpdateSourceTrigger="PropertyChanged" Mode="OneWayToSource">
        <Binding.ValidationRules>
            <my:NumberValidationRule ErrorMessage="Please enter a number in hours." />
        </Binding.ValidationRules>
    </Binding>
</TextBox>

HoursWorked对象如下所示:

//I have omitted a lot of things so it's more readable
public class HoursWorked : INotifyPropertyChanged
{

    private double hours;

    public HoursWorked()
    {
        hours = 0;
    }

    public double Hours
    {
        get { return hours; }
        set 
        {
            if (Hours != value)
            {
                hours = value;
                OnPropertyChanged("Hours");
            }
        }
    }

    #region Databinding
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(String info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
    #endregion
}

一旦窗口初始化,这就是我拥有的代码部分:

public partial class Blah : Window
{
     private HoursWorked newLog;

public Blah()
{
    InitializeComponent();
    newLog = new HoursWorked();
    adminTimeTxtBox.DataContext = newLog;
}

private void addAdBtn_Click(object sender, RoutedEventArgs e)
{
    AddHours();

}

private void AddHours()
{
    if (emp.UserType.Name == "Admin")
    {
        if(!ValidateElement.HasError(adminTimeTxtBox))
        {
                item.TimeLog.Add(newLog);
                UpdateTotals();
                adminTimeTxtBox.Clear();
        }
         }

    }
}

最后验证元素如下所示:

public static class ValidateElement
{
    public static bool HasError(DependencyObject node)
    {
        bool result = false;
        if (node is TextBox)
        {
            TextBox item = node as TextBox;
            BindingExpression be = item.GetBindingExpression(TextBox.TextProperty);
            be.UpdateSource();
        }
        if (Validation.GetHasError(node))
        {
            // If the dependency object is invalid, and it can receive the focus,
            // set the focus
            if (node is IInputElement) Keyboard.Focus((IInputElement)node);
            result = true;
        }

        return result;

    }
}

它正确验证,但每次检查属性是否更新时,它都没有。我真的需要帮助,非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你有2个HoursWorked类的实例。

一个是在资源中通过此标记<my:HoursWorked x:Key="hwViewSource" />创建的,但是您在窗口中使用newLog = new HoursWorked()创建一个;并将其设置为adminTimeTxtBox的DataContext ...因此您绑定到的(资源一)与您正在更新的那个(Window中的一个)不同。

您可以将绑定更改为

<Binding Source="{Binding}" ....

然后不需要Resource中定义的那个。

答案 1 :(得分:1)

TextBox.Text属性的类型为字符串,您的Hours属性为double。

您必须创建ValueConverter或辅助属性,以便将字符串解析为double,反之亦然。