是否可以根据 Silverlight 4 中的可绑定属性动态选择要渲染的控件?
我有一个带有 ItemTemplate 的 ListBox,它呈现一个具有两列的网格。第一列是 TextBlock,第二列是 ComboBox。
这个想法是向用户呈现一个问题列表和一个组合,用户可以从中选择答案。这与这个 xaml 一起工作正常:
<数据模板> <网格 d:DesignWidth="931" d:DesignHeight="61" d:IsLocked="True" 边距="0"> <数据模板> <文本框.背景> >
将 TextBox 放入 DataTemplate 中(而不是 TextBlock)的原因是我第一次尝试允许用户除了从下拉列表中进行选择之外还可以输入自由文本。不过,这是可行的,文本框位于组合框内。那不是我想要的。
是否可以基于某些可绑定属性渲染“普通”文本框而不是组合框?
因此,如果属性 InputType==FreeText 则使用 TextBox 呈现视图,如果属性为 Inputtype==Combo 则按上述方式呈现?
t。
I have a ListBox with an ItemTemplate which renders a Grid with two columns. The first column is a TextBlock and the second is a ComboBox.
The idea is to present to the user a list of questions and a Combo from which the user can choose an answer. This works ok with this xaml:
<ListBox x:Name="QAListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedIndex="-1" ItemsSource="{Binding Questions}" IsTabStop="True" TabIndex="5" ScrollViewer.HorizontalScrollBarVisibility="Auto" Margin="10" BorderThickness="0"> <ListBox.ItemTemplate> <DataTemplate> <Grid d:DesignWidth="931" d:DesignHeight="61" d:IsLocked="True" Margin="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width=".80*" MinWidth="800"/> <ColumnDefinition Width=".20*" MinWidth="200"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Path=QuestionText}" Padding="10" FontSize="21.333" FontWeight="Bold" Margin="0" Grid.Column="0" d:IsLocked="True" /> <ComboBox ItemsSource="{Binding Path=AnswerAlternative}" SelectedValue="{Binding Path=QuestionsAndAnswers}" SelectedValuePath="AnswerAlternativeId" FontSize="21.333" FontWeight="Bold" Grid.Column="1" Margin="60,0,0,0" d:IsLocked="True" SelectionChanged="ComboBox_SelectionChanged"> <ComboBox.ItemTemplate> <DataTemplate> <TextBox Text="{Binding Path=AnswerText, Mode=TwoWay}" BorderThickness="0"> <TextBox.Background> <SolidColorBrush /> </TextBox.Background> </TextBox> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
The reason for putting a TextBox inside the DataTemplate (in stead of TextBlock), is my first attempt on allowing the user to enter free text in addition to choosing from the dropdown. It kind of work, however, the TextBox is inside the ComboBox. That is not what I want.
Is it possible to have a "plain" TextBox render in stead of a ComboBox based upon some bindable attribute?
So that if an attribute InputType==FreeText the view is rendered with a TextBox and if the attribute is Inputtype==Combo it is rendered as above?
t.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
针对您的特定问题的一个简单解决方案是在 Visibility 属性上包含两者并使用值转换器:-
那么您的 Xaml 可能如下所示:-
更复杂的解决方案将使用
ListBox
的子类和override 或PrepareContainerForItemOverride
以允许分配各种 ItemTemplate。但我认为这对于这个问题来说有点过分了。A simplistic solution to your specific problem is to include both and use a value converter on the Visibility property:-
Then your Xaml can look like:-
More sophisticated solutions would use a sub-class of
ListBox
and an override orPrepareContainerForItemOverride
to allow variety of ItemTemplates to be assigned. However I think that would be overkill for this problem.