我正在做一个树视图UI。我使用DataTemplateSelector来决定是根据数据参数集合动态显示一系列文本框还是组合框。
请在我的代码中注明。ArugumentDetailsCollection是一个可观察的集合,包含ArgumentDetails类。DefaultValue是ArgumentDetails类中的字符串属性。请注意,该属性不是依赖项属性。
问题是DefaultValue没有绑定到TextBox。当显示TextBox时,它包含空字符串。
如果没有使用数据模板选择器,请注意文本框工作正常。有人能给我建议吗?谢谢
<ItemsControl x:Name="argumentTexts" ItemsSource="{Binding ArgumentDetailsCollection}">
<ItemsControl.Resources>
<DataTemplate x:Key="TextBoxDataTemplate">
<TextBox Text="{Binding Path=DefaultValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Center"
Width="Auto"
Margin="5,0,0,0"
Padding="0"
Style="{StaticResource GridEditStyle}"
IsEnabled="True"/>
</DataTemplate>
<DataTemplate x:Key="ComboBoxDataTemplate">
<ComboBox HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Width="Auto"
Margin="5,0,0,0"
Padding="0"
Style="{StaticResource GridEditStyle}"
IsEnabled="True"/>
</DataTemplate>
<columnConfiguratorControls:ArgumentTypeTemplateSelector x:Key="ArgTemplateSelector" ComboBoxDataTemplate="{StaticResource ComboBoxDataTemplate}" TextBoxDataTemplate="{StaticResource TextBoxDataTemplate}"/>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Stretch" IsItemsHost="True" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type structures:ArgumentDetails}">
<ContentControl Content="{Binding VisibleName}"
ContentTemplateSelector="{StaticResource ArgTemplateSelector}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>发布于 2015-03-06 15:50:19
在您的中写入Content=“{Binding}”将使所选DataTemplate的数据文本成为VisibleName属性。这就是为什么不能访问DefaultValue属性的原因,因为它是ArgumentDetails的成员。
将绑定更改为:
Content="{Binding}"您还需要检查您的ContentTemplateSelector类
https://stackoverflow.com/questions/28901447
复制相似问题