Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不影响列宽的WPF组合框_C#_Wpf_Wpf Controls - Fatal编程技术网

C# 不影响列宽的WPF组合框

C# 不影响列宽的WPF组合框,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,如何告诉组合框不影响其所在网格的列宽 下面是一个简单的例子: <StackPanel Orientation="Vertical"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="5"/> <Column

如何告诉组合框不影响其所在网格的列宽

下面是一个简单的例子:

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label>This is some text</Label>
        <Label Grid.Row="1">This is some text</Label>
        <GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Stretch"/>
        <Label Grid.Column="2" Background="Beige" HorizontalAlignment="Right">This is some text</Label>
        <ComboBox Grid.Row="1" Grid.Column="2">
            <ComboBoxItem IsSelected="True">This is some text</ComboBoxItem>
            <ComboBoxItem>This is some really lengthy text that is really long</ComboBoxItem>
        </ComboBox>
    </Grid>
</StackPanel>

这是一些文本
这是一些文本
这是一些文本
这是一些文本
这是一篇非常长的文章,非常长
当选择第二个项目时,组合框会改变其大小,同时第三列的大小也会随之改变(从标签的米色背景可以看出)。

这还具有米色标签中的文本有时位于可见区域之外的效果,即使有足够的空间完全显示它:


我想要的是,第三列的宽度始终为米色标签(或列中的任何其他元素,而不是组合框),组合框会缩短其文本,使其适合该宽度。组合框的弹出部分可以更大,我在这里只是说按钮部分。我尝试设置一个组合框,将TextBlock内容和TextTrimming设置为CharacterEllipsis,但没有效果。

设置组合框控件的MaxWidth。它将允许组合框扩展到该值。若你们想要一个固定的宽度,那个么你们需要设置组合框的宽度属性

您可以使用Combobox的宽度属性,如

<ComboBox Grid.Row="1" Grid.Column="2" Width="200">


此外,文本换行也可能对您有所帮助,下面的链接提供了一个示例,说明如何对combobox使用文本换行:

在这里,这应该可以帮助您:

<StackPanel Orientation="Vertical">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label>This is some text</Label>
        <Label Grid.Row="1">This is some text</Label>
        <GridSplitter Grid.Column="1" Grid.RowSpan="2" HorizontalAlignment="Stretch"/>
        <Label x:Name="Label" Grid.Column="2" Background="Beige" HorizontalAlignment="Right">This is some text</Label>
        <ComboBox Grid.Row="1" Grid.Column="2" Width="{Binding ElementName=Label, Path=ActualWidth, Mode=OneWay}">
            <ComboBoxItem IsSelected="True">This is some text</ComboBoxItem>
            <ComboBoxItem>This is some really lengthy text that is really long</ComboBoxItem>
        </ComboBox>
    </Grid>
</StackPanel>

这是一些文本
这是一些文本
这是一些文本
这是一些文本
这是一篇非常长的文章,非常长

以下是代码供您参考



设置组合框的
宽度
属性,或将
列定义的
宽度
设置为固定值,但这不是我想要的。我把GridSplitter放在那里,因为它在我的原始示例中。要求组合框与列中的所有其他元素一样宽。还有什么其他元素?在本例中,只有标签,但显然这只是一个最小的示例,实际上列中可能有其他元素。这不是我想要的。组合框应与列中的所有其他元素一样宽,而不是某个固定值。哪些元素在列中(不是在我的最小示例中)是在运行时决定的。然后执行以下操作,使用*设置列的宽度,并设置组合框HorizontalAlignment=“Stretch”。同样,这不是我想要的。要求组合框与列的宽度完全相同,但不影响列的大小。不允许固定宽度。这在示例中有效,谢谢。在我的实际应用程序中,整个列都是在运行时创建的,这并不容易,但我想我必须编写隐藏代码,在该列中选择一个随机的非ComboBox控件并绑定到该控件。为了给出一个结论,我只是在我的实际代码中实现了上面提到的隐藏代码,它可以按要求工作,所以再次谢谢你。@Flash没问题,很高兴它能帮上忙。