【问题标题】:Do we have to automatically widen the combo to the selected item?我们是否必须自动将组合扩大到所选项目?
【发布时间】: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 = "&quot;Title 3&quot;"/>
    </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 的组合框。

标签: c# wpf xaml combobox


【解决方案1】:

可能不完全是在寻找什么,但应该让你走上正确的道路

   <GridBox.ItemTemplate>
     <DataTemplate>
         <TextBlock Text="{Binding Path=TaskName}" TextWrapping="Wrap"/>
     </DataTemplate>
   </GridBox.ItemTemplate>

然后尝试将 TextBlock 的宽度绑定到另一个有效宽度和元素

Width="Stretch" 可能会起作用

【讨论】:

  • 谢谢。使用我的文本框控件让它工作。
【解决方案2】:

您可以在组合框上设置MaxWidth。在不知道其余标记的情况下,您可以将控件放在Grid 中。这将允许您设置ColumnWidth,以便我在屏幕截图中看到的其他控件具有相同的宽度。

【讨论】:

  • 谢谢。我已经把它放在一个网格中。我已经用我的标记更新了问题,以防它有帮助。
  • 您可以将两列的 ColumnDefinition 设置为“*”。这将使每一列的大小相同。
  • 我已将其更改为每个都使用 1*。它看起来更好。但我无法设置MaxWidth,因为我的列会调整大小。所以这个值已经过时了。
【解决方案3】:

听起来在你的 ComboBox 上设置一个MaximumWidth 会更好。

comboBox.MaxWidth = 150;

信息是here

【讨论】:

  • 谢谢,这行得通,但它与标记冲突。您看到的控件是可拉伸的。所以 MaxWidth 值需要随着容器宽度的变化而变化。我在问题中添加了标记。我们可以解决这个问题吗?
  • 我尝试覆盖网格调整大小事件,然后设置 maxwidth 属性,但没有成功。我认为因为它被设置了一次,所以它再也不会调整大小了。
  • 您还应该能够将大小本身设置为 Auto 并将 MaxWidth 设置为不同的值。您仍然希望它在不超过某个点的情况下改变大小,对吧?
  • 随着夹点的拖动,组合会改变宽度。但 MaxWidth 属性应始终与组合的当前宽度一致。
猜你喜欢
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 2022-10-21
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
相关资源
最近更新 更多