简体   繁体   English

C#/ WPF组合框模板化项目的高度

[英]C# / wpf combobox templated item height

I am making a user control that has a button and a combo box in it. 我正在制作一个带有按钮和组合框的用户控件。 The combo box is read-only. 组合框是只读的。 I have a DataTemplate defined and the ItemsSource in the combo box is set to a list of doubles which equate to percentages of a value that I have defined as a dependency property in the user control. 我定义了一个DataTemplate ,并且组合框中的ItemsSource设置为一个双精度列表,该列表等于我在用户控件中定义为依赖项属性的值的百分比。 My intent is to display as each item, the percentage of the value in the user control. 我的意图是在用户控件中显示每个值的百分比。

Everything works fine except that the height of the items when the combo box's drop down is open are about twice the height of the combo box itself, and when I pick one, it sets the height of the user control to the size of the combo box. 一切正常,除了打开组合框下拉菜单时项目的高度大约是组合框本身高度的两倍,当我选择一个时,它将用户控件的高度设置为组合框的大小。

Unopened Combo Box 未打开的组合框

Combo Box after selecting item 选择项目后的组合框

If I leave out the template, the items in the combo box are sized to the original height of the combo box and the combo box remains at it's original height. 如果我遗漏了模板,则组合框中的项目大小将调整为组合框的原始高度,并且组合框保持在其原始高度。

I would like the height of the items when the cobo's drop-down is opened to remain the size of the original combo box, and when I select an item from the drop-down, I do not wan the combo box to resize. 我希望打开cobo的下拉菜单时保持项目的高度,以保持原始组合框的大小,并且当我从下拉菜单中选择一个项目时,我不希望组合框调整大小。

Anyone have any idea how to accomplish this? 任何人都知道如何做到这一点?

XAML: 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#: 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;
        }
    }
}

When I originally posted this, I had tried all manner of binding the height of the label to the height of the combo box, the items control, etc, setting the Margin property, and on. 当我最初发布此内容时,我尝试了各种方式将标签的高度绑定到组合框,项目控件等的高度,设置Margin属性,然后打开。 After more searching, I found what works well enough for me. 经过更多搜索,我发现什么对我来说足够好。 I set the "Padding" property of the Label to 0. MS' documentation for the Control.Padding property says that the default value is 0. In this case, something must have set it to a value other than 0. So, setting the Padding property to 0 overrides the value it was set to. 我将Label的“ Padding”属性设置为0。MS的Control.Padding属性文档说默认值是0。在这种情况下,必须将其设置为非0的值。将填充属性设置为0将覆盖其设置的值。 When selecting the item, it does not resize the combobox. 选择项目时,它不会调整组合框的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM