首页 文章

C#/ wpf组合框模板项目高度

提问于
浏览
0

我正在制作一个用户控件,里面有一个按钮和一个组合框 . 组合框是只读的 . 我有一个 DataTemplate 定义,组合框中的 ItemsSource 设置为双精度列表,它等于我在用户控件中定义为依赖属性的值的百分比 . 我的目的是显示每个项目,用户控件中值的百分比 .

一切正常,但组合框下拉打开时项目的高度大约是组合框本身高度的两倍,当我选择一个时,它将用户控件的高度设置为组合框的大小 .

Unopened Combo Box

Combo Box after selecting item

如果我省略模板,组合框中的项目大小为组合框的原始高度,组合框保持原始高度 .

当打开cobo的下拉菜单以保持原始组合框的大小时,我想要项目的高度,当我从下拉列表中选择一个项目时,我不想让组合框调整大小 .

任何人都知道如何做到这一点?

XAML:

<UserControl x:Class="FEAServer.UI.Controls.TorqueControl" Name="theControl"
        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:valconv="clr-namespace:FEAServer.Common.ValueConverters;assembly=FEAServer.Common.ValueConverters"
        xmlns:localconv="clr-namespace:FEAServer.UI.Controls.Converters"
        xmlns:localprops="clr-namespace:FEAServer.UI.Controls.Properties"
        mc:Ignorable="d">
<UserControl.Resources>
    <valconv:UnitsOfMeasureConverter x:Key="UOMConv" />
    <localconv:TorquePercentageConverter x:Key="TorquePCTConv"/>
    <DataTemplate x:Key="LoadCasesDataTemplate">
        <Label>
            <Label.Content>
                <MultiBinding Converter="{StaticResource TorquePCTConv}" ConverterParameter="{x:Static valconv:UnitsOfMeasureUnit.TORQUE}">
                    <Binding ElementName="theControl" Path="UnitsOfMeasure"/>
                    <Binding />
                    <Binding ElementName="theControl" Path="AnalysisTorque" />
                    <Binding Source="{StaticResource UOMConv}" />
                    <Binding Source="{x:Static localprops:Resources.torqueFormatString}"/>
                </MultiBinding> 
            </Label.Content>
        </Label>
    </DataTemplate>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <ComboBox Name="cbSelectTorque" Height="Auto" Grid.Column="0" IsReadOnly="True"
              HorizontalAlignment="Stretch" ItemsSource="{Binding TorquePercentages}"
              VerticalAlignment="Center" ItemTemplate="{StaticResource LoadCasesDataTemplate}"
              Margin="3,3,0,3">
    </ComboBox>
    <Button Name="btnUnits" Width="Auto" Height="{Binding ElementName=cbSelectTorque, Path=Height}"
            Grid.Column="1" Margin="3" Click="btnUnits_Click">
        <Button.Content>
            <MultiBinding Converter="{StaticResource UOMConv}" ConverterParameter="{x:Static Member=valconv:UnitsOfMeasureUnit.TORQUE}">
                <MultiBinding.Bindings>
                    <Binding Path="UnitsOfMeasure" Mode="OneWay" />
                </MultiBinding.Bindings>
            </MultiBinding>
        </Button.Content>
    </Button>
</Grid>

C#:

public partial class TorqueControl : UserControl
{
    bool _isTorqueConfigurable;

    public static DependencyProperty UnitsOfMeasureProperty = DependencyProperty.
        Register("UnitsOfMeasure", typeof(UnitsOfMeasureSystem), typeof(TorqueControl),
        new PropertyMetadata(new PropertyChangedCallback(OnUnitsOfMeasureChanged)));

    public static DependencyProperty AnalysisTorqueProperty = DependencyProperty.
        Register("AnalysisTorque", typeof(double), typeof(TorqueControl),
        new PropertyMetadata(new PropertyChangedCallback(OnAnalysisTorqueChanged)));

    public static DependencyProperty TorquePercentagesProperty = DependencyProperty.
        Register("TorquePercentages", typeof(ObservableCollection<double>), typeof(TorqueControl),
        new PropertyMetadata(new PropertyChangedCallback(OnTorquePercentagesChanged)));

    public TorqueControl()
    {
        this.DataContext = this;
        InitializeComponent();
    }

    public UnitsOfMeasureSystem UnitsOfMeasure
    {
        get
        {
            return (UnitsOfMeasureSystem)GetValue(UnitsOfMeasureProperty);
        }
        set
        {
            if ((UnitsOfMeasureSystem)GetValue(UnitsOfMeasureProperty) != value)
            {
                SetValue(UnitsOfMeasureProperty, value);
            }
        }
    }

    static void OnUnitsOfMeasureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TorqueControl cont = d as TorqueControl;
        if(cont != null)
        {
            cont.UnitsOfMeasure = (UnitsOfMeasureSystem)e.NewValue;
        }
    }

    public double AnalysisTorque
    {
        get
        {
            return (double)GetValue(AnalysisTorqueProperty);
        }
        set
        {
            if ((double)GetValue(AnalysisTorqueProperty) != value)
            {
                SetValue(AnalysisTorqueProperty, value);
            }
        }
    }

    static void OnAnalysisTorqueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TorqueControl cont = d as TorqueControl;
        if (cont != null)
        {
            cont.AnalysisTorque = (double)e.NewValue;
        }
    }

    public ObservableCollection<double> TorquePercentages
    {
        get
        {
            return (ObservableCollection<double>)GetValue(TorquePercentagesProperty);
        }
        set
        {
            if ((ObservableCollection<double>)GetValue(TorquePercentagesProperty) != value)
            {
                SetValue(TorquePercentagesProperty, value);
            }
        }
    }

    static void OnTorquePercentagesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TorqueControl cont = d as TorqueControl;
        if (cont != null)
        {
            cont.TorquePercentages = (ObservableCollection<double>)e.NewValue;
        }
    }

    public bool IsTorqueConfigurable
    {
        get
        {
            return _isTorqueConfigurable;
        }
        set
        {
            if (_isTorqueConfigurable != value)
            {
                _isTorqueConfigurable = value;
            }
        }
    }
    private void btnUnits_Click(object sender, RoutedEventArgs e)
    {
        if (UnitsOfMeasure == UnitsOfMeasureSystem.ENGLISH)
        {
            UnitsOfMeasure = UnitsOfMeasureSystem.METRIC;
        }
        else
        {
            UnitsOfMeasure = UnitsOfMeasureSystem.ENGLISH;
        }
    }
}

1 回答

  • 0

    当我最初发布这个时,我已尝试将标签的高度绑定到组合框的高度,项目控件等,设置Margin属性等等 . 经过更多的搜索,我发现什么对我有用 . 我将Label的“Padding”属性设置为0.对于Control.Padding属性的MS'文档说默认值为0.在这种情况下,必须将某些内容设置为0以外的值 . 因此,设置Padding属性为0会覆盖它设置的值 . 选择项目时,它不会调整组合框的大小 .

相关问题