c# - 从MVVM中的代码设置WPF延迟

标签 c# wpf xaml mvvm caliburn.micro

我正在使用WPF应用程序(.NET 4.5),并使用Caliburn.Micro。我在ListBox中生成问题和答案的列表,答案可以是3种类型(RadioButton,DropDowns和Textboxes)。回答问题时,属性触发器将在ViewModel中更改,并添加下一个问题。
我遇到的问题是,将符号添加到Textbox时,它立即触发属性更改。

public string TextValue
    {
        get { return _textValue; }
        set
        {
            _textValue = value;
            NotifyOfPropertyChange(() => TextValue);
        }
    }

通常(对于非动态生成的控件),我可以通过使用“new”来延迟它
<TextBlock Text="{Binding TextValue, Delay=500}"/>
但是由于产生这些问题,我不确定如何进行此操作。

是否可以设置Delay以从后面的代码生成控件?

更新:
这就是XAML的样子。在运行时(来自DB),列表中填充了问题,并且问题确实会根据先前的答案而发生变化,因此无法在XAML中进行任何设置。
<UserControl x:Class="Corp.Conveyancing.Desktop.Views.Probate.PifWFlowQuestionsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:probate="clr-namespace:Corp.Conveyancing.Desktop.Views.Probate"
             mc:Ignorable="d" >
    <Grid Margin="10" Width="600" Height="400" >
        <ListBox x:Name="QuestionItems" Grid.Row="0" BorderThickness="0" HorizontalContentAlignment="Stretch" ScrollViewer.CanContentScroll="true" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="380" Width="580" probate:ListBoxExtenders.AutoScrollToEnd="True" >
            <ListBox.ItemContainerStyle >
                <Style TargetType="ListBoxItem">
                    <Setter Property="Focusable" Value="False"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</UserControl>

UPDATE2:
我有BindableCollection<QuestionItemViewModel>,它是PifWFlowQuestionsViewModel中的属性,在其中我仅添加第一个问题,然后根据答案添加更多问题,并基于那些添加更多问题的依此类推。
public class PifWFlowQuestionsViewModel : PropertyChangedBase
{
    private BindableCollection<QuestionItemViewModel> _questionItems =
        new BindableCollection<QuestionItemViewModel>();

    public BindableCollection<QuestionItemViewModel> QuestionItems
    {
        get { return _questionItems; } 
        set
        {
            _questionItems = value;
            NotifyOfPropertyChange(() => QuestionItems);
        }
    }
}

最佳答案

这就是我最终解决它的方式,可能不是THE解决方案,但是它既快速又容易。
我意识到它不能回答问题的标题,但是它确实为上述问题提供了解决方案,并且我相信有人会发现这很有用。

public class QuestionTextViewModel : QuestionItemViewModel
{
    private Timer Timer { get; set; }

    public QuestionTextViewModel(IEventAggregator eventAggregator, TransactionDetail transactionDetail)
        : base(transactionDetail)
    {
        _eventAggregator = eventAggregator;
        TransactionDetailId = transactionDetail.TransactionDetailId;
        this.Timer = new Timer(1500) {AutoReset = false};
        this.Timer.Elapsed += TextValueTimer_Elapsed;
    }

    public string TextValue
    {
        get { return _textValue; }
        set
        {
            _textValue = value;
            this.Timer.Stop();
            this.Timer.Start();
        }
    }

    private void TextValueTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        NotifyOfPropertyChange(() => TextValue);
    }
}

关于c# - 从MVVM中的代码设置WPF延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25886800/

相关文章:

c# - 以编程方式添加控件时未实例化占位符

c# - 有没有办法查看 System.Net.Mail 是否有效

c# - 使用动态类型

c# - 动态添加文本 block 到网格

c# - 知道为什么这个 RavenDB 查询只返回 6 个结果吗?

c# - 双向绑定(bind)到 WPF 中多个列表框上的 ListBox SelectedItem

c# - Gridsplitter 未显示

c# - 如何结合异步调用对进度条进行编程?

c# - 两侧具有不同笔划粗细的 WPF 矩形或具有虚线笔划的边框?

c# - 在ASP.Net中,同组的单选按钮会自动改变Checked状态吗?