【问题标题】:Setting WPF Delay from code in MVVM从 MVVM 中的代码设置 WPF 延迟
【发布时间】:2014-09-17 09:24:03
【问题描述】:

我正在开发 WPF 应用程序 (.NET 4.5),并使用 Caliburn.Micro。我正在生成ListBox 中的问题和答案列表,答案可以是 3 种类型(RadioButtons、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>

更新 2:
我有BindableCollection&lt;QuestionItemViewModel&gt;,这是PifWFlowQuestionsViewModel 中的属性,我只添加第一个问题,然后根据答案添加更多问题,并基于添加更多问题等等。

public class PifWFlowQuestionsViewModel : PropertyChangedBase
{
    private BindableCollection<QuestionItemViewModel> _questionItems =
        new BindableCollection<QuestionItemViewModel>();

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

【问题讨论】:

  • 如何为 ListBox 生成项目?键入推理和数据模板或通过代码隐藏手动生成控件?
  • @toadflakz 我有BindableCollection&lt;QuestionItemViewModel&gt; 这是PifWFlowQuestionsViewModel 中的属性,我只添加第一个问题,然后根据答案我添加更多问题,并且基于那些添加更多等等开。
  • 我遇到的问题是,当符号被添加到文本框时,它会立即触发属性更改...为什么这是个问题?
  • @Sheridan 在每次按键后添加下一个问题,并专注于它。
  • 那你一定是设置了一些Binding.UpdateSourceTriggerPropertyChanged...改成LostFocus,这样它只会在输入完成时更新。

标签: c# wpf xaml mvvm caliburn.micro


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您应该在ListBox 上使用ItemTemplateSelector,并根据新项目的返回属性确定DataTemplate 使用的类型。

    当您指定 DataTemplate 内容时,肯定可以在 XAML 中生成控件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多