【发布时间】:2016-06-17 17:49:16
【问题描述】:
感谢这位出色的 article 我在 binding ComboBox 方面取得了良好的进展。
但是我有一个问题,我不知道这是否可以用 WPF 解决。
这是课程:
namespace OCLMEditor
{
/// This class provides us with an object to fill a ComboBox with
/// that can be bound to string fields in the binding object.
public class ComboBoxItemString
{
public string ValueString { get; set; }
}
}
在我的 App.XAML 文件中,我有以下资源:
<Application.Resources>
<x:Array x:Key="SongTitleString" Type="local:ComboBoxItemString">
<local:ComboBoxItemString ValueString = "Title 1"/>
<local:ComboBoxItemString ValueString = "Title 2"/>
<local:ComboBoxItemString ValueString = ""Title 3""/>
</x:Array>
</Application.Resources>
该数组中有 154 个项目。当我运行应用程序并从列表中选择一个项目时,会发生这种情况。
之前:
如您所见,组合列表比实际的组合框更宽。但我对此很好。但是当您选择该项目时,之后它看起来像:
组合是这样声明的:
<ComboBox ItemsSource="{StaticResource SongTitleString}"
DisplayMemberPath="ValueString"
SelectedValuePath="ValueString"
SelectedValue="{Binding SongTitleString}" />
我不希望它是可编辑的。那么,当我们从组合中选择一个条目时,有什么方法不会扩大实际组合本身?如果可能的话,(所以它显示剪辑的文本)?
当我使用可编辑组合时也会发生这种情况。示例:
额外的标记信息:
<GroupBox Grid.Row="1" Margin="5,5,10,5" Background="WhiteSmoke">
<Grid Height="auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Margin="5,0,0,0">
<Label>Week of Meeting:</Label>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<ComboBox>
<ComboBoxItem>
<StackPanel Orientation="Horizontal" Margin="0">
<Image Source="special_event.png"></Image>
<Label>Date 1</Label>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem>
<StackPanel Orientation="Horizontal" Margin="0">
<Image Source="special_event.png"></Image>
<Label>Date 2</Label>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem>
<StackPanel Orientation="Horizontal" Margin="0">
<Image Source="special_event.png"></Image>
<Label>Date 3</Label>
</StackPanel>
</ComboBoxItem>
</ComboBox>
<Image Grid.Column="1" HorizontalAlignment="Right" Source="event_time.png" Margin="2"></Image>
</Grid>
<Label>Note:</Label>
<ComboBox IsEditable="True" ItemsSource="{StaticResource NoteString}"
DisplayMemberPath="ValueString"
SelectedValuePath="ValueString"
SelectedValue="{Binding Note}" />
<Label>Bible Reading for Week:</Label>
<TextBox>PSALMS 60-68</TextBox>
<Label>Opening Song:</Label>
<ComboBox ItemsSource="{StaticResource SongTitleString}"
DisplayMemberPath="ValueString"
SelectedValuePath="ValueString"
SelectedValue="{Binding SongTitle}" />
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Vertical" Margin="10,0,10,0">
<Label>Chairman:</Label>
<ComboBox></ComboBox>
<Label>Auxiliary Counsellor 1:</Label>
<ComboBox></ComboBox>
<Label>Auxiliary Counsellor 2:</Label>
<ComboBox></ComboBox>
<Label>Prayer:</Label>
<ComboBox></ComboBox>
</StackPanel>
</Grid>
</GroupBox>
此链接:WPF how to make Grid Reevaluate Max Widths on GridSplitter Column Resize?
有答案:
MaxWidth="{Binding ElementName=SignalValuesGridSplitter,Path=ActualWidth}" TextWrapping="Wrap"/>
所以我可能会遵循这个想法,但无法实现。
谢谢。
调整大小:
更新接受的答案:
<ComboBox.MaxWidth>
<Binding Path="ActualWidth"
ElementName="textWeeklyBibleReading"/>
</ComboBox.MaxWidth>
【问题讨论】:
-
这很奇怪。如果您可以使用更简单的示例进行重现,那将有所帮助。没有人会尝试复制它。我会查看 ItemTemplate 的组合框。