【发布时间】: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<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);
}
}
}
【问题讨论】:
-
如何为 ListBox 生成项目?键入推理和数据模板或通过代码隐藏手动生成控件?
-
@toadflakz 我有
BindableCollection<QuestionItemViewModel>这是PifWFlowQuestionsViewModel中的属性,我只添加第一个问题,然后根据答案我添加更多问题,并且基于那些添加更多等等开。 -
我遇到的问题是,当符号被添加到文本框时,它会立即触发属性更改...为什么这是个问题?
-
@Sheridan 在每次按键后添加下一个问题,并专注于它。
-
那你一定是设置了一些
Binding.UpdateSourceTrigger到PropertyChanged...改成LostFocus,这样它只会在输入完成时更新。
标签: c# wpf xaml mvvm caliburn.micro