私のコンボボックスは、ウィンドウ幅全体に水平方向に拡大する必要があります。これは、コンボボックスのドロップダウン ボタンとドロップダウン メニューも説明します。そのため、コンボボックスの幅を「自動」に設定し、ItemsPanelTemplate の幅をコンボボックスの幅にバインドします。アイテムにはテキスト トリミング データ テンプレートも使用します。
アプリケーションを起動すると、ドロップダウン メニューの幅がコンボ ボックスのボタンの幅に対応し、テキストのトリミングも機能します。実行時にウィンドウのサイズを変更すると、次の動作が発生します。
- ウィンドウのサイズを大きく変更 -> ドロップダウン ボタンとドロップダウン メニューのサイズを正しく変更 (ウィンドウの幅全体に完全に引き伸ばされたまま)
- ウィンドウのサイズを小さく変更 -> ドロップ ダウン ボタンは正しくサイズ変更されますが、ドロップ ダウン メニューは最初のサイズよりも小さくサイズ変更されません。そのため、ウィンドウを非常に小さくドラッグすると、ドロップダウン メニューが切り取られます。
実行時にドロップダウンメニューボタンに応じてドロップダウンメニューの幅を変更するにはどうすればよいですか?
<UserControl.Resources>
<DataTemplate x:Key="ComboBoxCustomTemplate">
<TextBlock Text="{Binding Name}" TextTrimming="WordEllipsis"/>
</DataTemplate>
<ItemsPanelTemplate x:Key="ComboBoxItemsPanel">
<VirtualizingStackPanel Orientation="Vertical" x:Name="iptCombo"
Width="{Binding ActualWidth, ElementName=radComboBox, Mode=OneWay}" HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</UserControl.Resources>
<Grid x:Name="ImageView" Margin="5">
<telerik:RadComboBox x:Name="radComboBox"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Width="Auto"
IsEditable="False"
ItemsSource="{Binding Items}"
ItemsPanel="{StaticResource ComboBoxItemsPanel}"
ItemTemplate="{StaticResource ComboBoxCustomTemplate}"
SelectedIndex="0" >
</telerik:RadComboBox>
</Grid>