Textbox.Text输入没有约束力物业

问题描述:

我有一个对象被创建,并希望通过该模式绑定到该对象的属性OneWayToSource明确。然而,这种绑定根本不起作用。当程序初始化时,它在文本框右侧也有一个红色边框,当我点击按钮时,只希望输入有效。我最后一次沟通是将源头嵌入元素本身,但没有这样的运气。以下是我有:Textbox.Text输入没有约束力物业

<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(); 
     } 
     } 

    } 
} 

和最后ValidateElement看起来是这样的:

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个HoursWorked类的实例。

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

您可以更改绑定到

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

再不需要在资源定义的。

+0

哦,没关系,工作。我只是想知道,如果我只是使用了资源之一,是否意味着我不必创建一个新的HoursWorked()实例?我将如何能够将这个特定的对象添加到集合中?我将如何参考它? – Erika 2012-07-30 22:42:23

+0

如果你想引用在你的资源中创建的实例...那么你可以在你的代码隐藏中使用它的关键字来做FindResource ...然后你会引用同一个实例....但是你'坚持StaticResource绑定。 – 2012-07-30 22:45:05

TextBox.Text属性是字符串类型,你Hours性能加倍。

你必须解析字符串倍增,反之亦然创建ValueConverter或辅助性质。