首页 > 解决方案 > 如何阻止文本框在 wfp 中无限扩展

问题描述

我有以下控制:

在此处输入图像描述

由以下代码定义:

<UserControl x:Class="VariantMeshEditor.Views.EditorViews.Util.BrowsableItemView"
             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:common="clr-namespace:CommonDialogs.Common;assembly=CommonDialogs"
             mc:Ignorable="d" 
             x:Name="self">
    <UserControl.Resources>
        <common:BoolToVisibilityConverter x:Key="BoolToHiddenConverter" TrueValue="Visible" FalseValue="Collapsed" />
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition MaxHeight="50" Height="auto"></RowDefinition>
        </Grid.RowDefinitions>

        <Label Grid.Column="0"  Content="{Binding LabelName, ElementName=self}" Width="{Binding LabelLength, ElementName=self}"/>
        <Label Grid.Column="1">:</Label>

        <CheckBox Grid.Column="2" x:Name="CheckBox" Visibility="Collapsed" VerticalAlignment="Center"></CheckBox>
        <Border x:Name="b" Grid.Column="3"/>
        <TextBox  Grid.Column="3" IsReadOnly="{Binding PathTextReadOnly, ElementName=self}" TextWrapping="NoWrap" VerticalAlignment="Center" Text="{Binding PathText, ElementName=self}" Width="{Binding ActualWidth, ElementName=b}"/>

        <Button Grid.Column="4" Visibility="{Binding DisplayRemoveButton,  ElementName=self, Converter={StaticResource BoolToHiddenConverter}}" Command="{Binding Remove, ElementName=self}" CommandParameter="{Binding}" DockPanel.Dock="Right" Width="50" >Remove</Button>
        <Button Grid.Column="5" Visibility="{Binding DisplayBrowseButton,  ElementName=self, Converter={StaticResource BoolToHiddenConverter}}" Command="{Binding Browse, ElementName=self}" CommandParameter="{Binding}" DockPanel.Dock="Right" Width="50" >Browse</Button>
        <Button Grid.Column="6" Visibility="{Binding DisplayPreviewButton, ElementName=self, Converter={StaticResource BoolToHiddenConverter}}" Command="{Binding Preview, ElementName=self}" CommandParameter="{Binding}" DockPanel.Dock="Right" Width="50" >Preview</Button>
    </Grid>
</UserControl>

我的问题是,当我展开使用该控件的控件然后再次缩小时,TextBox 不会变小。关于如何解决这个问题的任何想法?我想将标签始终固定在左侧,将按钮固定在右侧,Texbox 占据剩下的所有内容。

普通的: 在此处输入图像描述

扩展: 在此处输入图像描述

再次变小: 在此处输入图像描述

标签: c#wpf

解决方案


您发布的 XAML 没有任何明显错误,并且似乎工作正常。我创建了一个没有任何绑定或我们无权访问的代码的最小示例,如下所示。如果您将其粘贴到一个干净的 WPF 应用程序中,您会看到它可以毫无问题地扩展和收缩。

这意味着问题可能出在您尚未在某处共享的代码中(或者我只是错过了它)。如果您可以修改此示例以显示问题,那么有人可能会帮助您。

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <DockPanel>
        <UserControl
             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"
             mc:Ignorable="d" 
             x:Name="self">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                    <ColumnDefinition Width="auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition MaxHeight="50" Height="auto"></RowDefinition>
                </Grid.RowDefinitions>

                <Label Grid.Column="0"  Content="Label" Width="40"/>
                <Label Grid.Column="1">:</Label>

                <CheckBox Grid.Column="2" x:Name="CheckBox" Visibility="Collapsed" VerticalAlignment="Center"></CheckBox>
                <Border x:Name="b" Grid.Column="3"/>
                <TextBox  Grid.Column="3" IsReadOnly="False" TextWrapping="NoWrap" VerticalAlignment="Center" Text="Type here"/>

                <Button Grid.Column="4" DockPanel.Dock="Right" Width="50" >Remove</Button>
                <Button Grid.Column="5" DockPanel.Dock="Right" Width="50" >Browse</Button>
                <Button Grid.Column="6" DockPanel.Dock="Right" Width="50" >Preview</Button>
            </Grid>
        </UserControl>
    </DockPanel>
</Window>

推荐阅读