【发布时间】:2011-09-29 15:01:51
【问题描述】:
我在 WPF 窗口上有一个 ComboBox,这让我有些心痛,因为当第一次显示时,第一个选择不正确地呈现。 ComboBox 不只显示文本;它显示从 UserControl 继承的类型的对象。这是 ComboBox 本身的 XAML:
<ComboBox Grid.Column="0"
Height="69"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
ItemsSource="{Binding Path=ViewChoices,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
Mode=OneWay}"
Margin="10"
Name="ViewPicker"
SelectionChanged="ViewPicker_SelectionChanged"
VerticalAlignment="Stretch"
VerticalContentAlignment="Center" />
如您所见,ComboBox 的 ItemsSource 绑定到拥有它的 UserControl 的一个名为 ViewChoices 的属性。 ViewChoices 对象是一个 ObservableCollection。
ComboBox 的内容在后面代码中的代码中设置,因为最终代码中的确切内容将从 XML 文件中读取;这些值现在是硬编码的。本质上,为读取的每个 XML 行创建一个 CameraViewChoice 对象,并将其添加到 ViewChoices 集合中。这一切都发生在 UserControl 的默认构造函数中,在调用 InitializeComponent() 之后。在 UserControl 的 Loaded 事件处理程序中,我有将 ComboBox 的 SelectedIndex 属性设置为 0 的代码。
CameraViewChoice 对象源自 UserControl。这是此控件的 Xaml:
<UserControl x:Class="CarSystem.CustomControls.CameraViewChoice"
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:cs="clr-namespace:CarSystem.CustomControls"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="50">
<Border BorderBrush="Black" BorderThickness="1">
<Grid>
<Image Opacity="0.35" Source="..." Stretch="Uniform" />
<Label Content="{Binding Path=Text}"
FontSize="18"
FontWeight="Bold"
Foreground="White"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
</Border>
</UserControl>
这是 CameraViewChoice 对象的代码隐藏:
public partial class CameraViewChoice : UserControl {
public static readonly DependencyProperty AttachedCameraProperty = DependencyProperty.Register( "AttachedCamera", typeof( Camera ), typeof( CameraViewChoice ), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.AffectsRender ) );
public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof( string ), typeof( CameraViewChoice ), new FrameworkPropertyMetadata( string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsParentMeasure | FrameworkPropertyMetadataOptions.AffectsRender ) );
public Camera AttachedCamera {
get { return (Camera) GetValue( AttachedCameraProperty ); }
set { SetValue( AttachedCameraProperty, value ); }
}
public string Text {
get { return (string) GetValue( TextProperty ); }
set { SetValue( TextProperty, value ); }
}
public CameraViewChoice() {
InitializeComponent();
}
一切正常,但有一个问题。当程序开始运行,第一次显示ComboBox时,选中的项显示全错。标签为空白,CameraViewChoice 控件显示过大,以至于底部被切断。我看到的是一个空白的 CameraViewChoice 对象,没有缩放到 ComboBox。
如果我在列表中选择一个项目,列表中的所有选项都会正确显示并且大小合适并且看起来很好,包括所选的选项。问题仅在于窗口首次显示时。
有人对正在发生的事情有任何想法吗?
托尼
编辑:
我在 Google 和 MSDN 杂志上做了一些研究,发现了 Josh Smith 的一篇关于数据模板的文章。从那里,我对我的 ComboBox 的 XAML 进行了以下更改:
<ComboBox Grid.Column="0"
Height="69"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
ItemsSource="{Binding Path=ViewChoices,
RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
Mode=OneWay}"
Margin="10"
Name="ViewPicker"
SelectionChanged="ViewPicker_SelectionChanged"
VerticalAlignment="Stretch"
VerticalContentAlignment="Center">
<ComboBox.ItemTemplate>
<DataTemplate>
<cs:CameraViewChoice Margin="10" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
这样更好,因为 ComboBox 中的项目不会改变大小,但初始显示仍然太大。也就是说,它在底部被切断。此外,所选项目的大小始终太大。因此,当您在列表中选择一项时,它会在组合框中显示部分剪裁。
我希望选择显示在 ComboBox 中,并且所有选项都适合它的一侧。对 CombobBox 的 ItemTemplate 的更改有何建议?
【问题讨论】:
-
你能拍几张它之前和之后的样子吗?
-
"在 UserControl 的 Loaded 事件处理程序中,我有代码将 ComboBox 的 SelectedIndex 属性设置为 0。"--- 删除它看看会发生什么。
-
您能否发布 ComboBox 的其余 xaml 代码。由于您使用“RelativeSource AncestorType”,因此 UserControl CameraViewChoice 应该包含组合框,但我们看不到代码。
-
@seekle:我包含了 ComboBox 控件的所有 Xaml。我不想在该部分中包含 Xaml 的其余部分,因为它有很多而且不相关。
-
@Bolu:我已经从 Loaded 事件处理程序中删除了代码。当 ComboBox 显示时,没有选择任何内容。所以当然,当我从下拉列表中选择一些东西时,一切都被找到了。但问题是初始状态由索引 0 中的选择表示;在此 ComboBox 中选择任何内容是没有意义的。
标签: wpf user-controls combobox