我有一个事件(ctc)在ComboBox中发生文本更改时触发,我想将其触发延迟一秒钟.我到目前为止编写了这段代码并将其放入MainWindow
:
Timer aTimer = new Timer();
aTimer.Interval = 1000;
aTimer.Elapsed += new ElapsedEventHandler(ctc);
aTimer.Enabled = true;
Run Code Online (Sandbox Code Playgroud)
我是WPF的新手,我想知道在ElapsedEventHandler
括号中放什么,我把偶数名称但是我收到了错误.我还需要为ComboBox的Xaml代码添加任何内容吗?
嗯,最简单的方法是使用Delay
@ASh提到的属性.我之前不知道,但我尝试了,这太神奇了:
XAML:
<ComboBox IsEditable="True" Text="{Binding ComboBoxText, Mode=OneWayToSource, Delay=1000}">
<ComboBoxItem Content="item1" />
<ComboBoxItem Content="item2" />
<ComboBoxItem Content="item3" />
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
视图模型:
private string comboBoxText;
public string ComboBoxText
{
get { return this.comboBoxText; }
set
{
if (this.SetProperty(ref this.comboBoxText, value))
{
Trace.WriteLine("*** New text: " + value);
// RunDatabaseSearch(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
SetProperty
实施在哪里INotifyPropertyChanged
.
在Visual Studio中观察输出窗口,文本将在用户最后一次输入后显示.
归档时间: |
|
查看次数: |
2735 次 |
最近记录: |