2014-04-18 41 views
1

我被困在一个奇怪的问题上。 我有一个AutoCompleteBox在我看来AutoCompleteBox.Text属性表现怪异

<sdk:AutoCompleteBox x:Name="txtSIA" 
Grid.ColumnSpan="1" Grid.Row="1" Grid.Column="1" 
SelectedItem="{Binding SIA, Mode=TwoWay, ValidatesOnNotifyDataErrors=True}" 
Text="{Binding TextSIA, Mode=TwoWay}" 
KeyUp="TxtSIA_KeyUp" 
Populating="SIANonSIU_Populating" 
Style="{StaticResource AutoCompleteStyle}" 
/> 

我实现了一个字段验证是检查其文字不为空或空字符串。 它工作得很好,但棘手的问题是,我有一个按钮,复位我的所有控件的值,它的代码从我的视图模型为:

void BtnReset_OnClick(RoutedEventArgs e) 
{ 
    SIA = new SIA(); 
    TextSIA = string.Empty; 
    BtnGeneralIsEnabled = false; 
    DataGridSource = null; 
} 

每当我点击它,然后在我的AutoCompleteBox写的AutoCompleteBox是即使在我的代码中,在关键事件监听器中也不会空或空。

这里有一些图片来说明我的观点:

Text = ab

Text = a

​​

回答

0

我找到了答案here

为了解决这个问题,但我们必须创建一个新的autocompletebox并重写OnApplyTemplate方法。

public class CustomAutoComplete : AutoCompleteBox 
{ 
TextBox mytext; 

public override void OnApplyTemplate() 
{ 
    base.OnApplyTemplate(); 
    mytext = GetTemplateChild("Text") as TextBox; 
    mytext.TextChanged += new System.Windows.Controls.TextChangedEventHandler(mytext_TextChanged); 
} 

void mytext_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) 
{ 
    this.Text = mytext.Text; 
    OnTextChanged(new RoutedEventArgs()); 
} 

}

0

注册到AutoCompleteBox而不是KEYUP的TextChanged事件:

“在AutoC的文本框部分中显示文本时发生ompleteBox更改。“

+0

谢谢你的建议,但它似乎没有解决这个问题.. –

+0

尝试通过e.Source,而不是发送铸造文本框。 TextSIA属性是否实现INotifyPropertyChanged? –

+0

仍然没有解决它。 真正的交易是,除了点击重置按钮之外,它工作正常。 –