C# C/wpf组合框模板项目高度
我正在制作一个用户控件,其中有一个按钮和一个组合框。组合框是只读的。我定义了一个DataTemplate,组合框中的ItemsSource被设置为一个双倍列表,它等于我在用户控件中定义为依赖项属性的值的百分比。我的目的是将用户控件中值的百分比显示为每个项 除了当组合框的下拉菜单打开时,项目的高度大约是组合框本身高度的两倍,并且当我选择一个时,它会将用户控件的高度设置为组合框的大小之外,一切都正常 如果我不使用模板,组合框中的项目将调整为组合框的原始高度,并且组合框将保持其原始高度 我希望打开cobo下拉列表时项目的高度保持原始组合框的大小,并且当我从下拉列表中选择项目时,我不希望组合框调整大小 有人知道如何做到这一点吗 XAML: C:C# C/wpf组合框模板项目高度,c#,wpf,combobox,C#,Wpf,Combobox,我正在制作一个用户控件,其中有一个按钮和一个组合框。组合框是只读的。我定义了一个DataTemplate,组合框中的ItemsSource被设置为一个双倍列表,它等于我在用户控件中定义为依赖项属性的值的百分比。我的目的是将用户控件中值的百分比显示为每个项 除了当组合框的下拉菜单打开时,项目的高度大约是组合框本身高度的两倍,并且当我选择一个时,它会将用户控件的高度设置为组合框的大小之外,一切都正常 如果我不使用模板,组合框中的项目将调整为组合框的原始高度,并且组合框将保持其原始高度 我希望打开co
当我最初发布这篇文章时,我尝试了各种方式将标签的高度绑定到组合框的高度、条目控件等,设置Margin属性等等。经过更多的搜索,我找到了适合我的。我将标签的Padding属性设置为0。MS的Control.Padding属性文档说明默认值为0。在这种情况下,必须有人将其设置为0以外的值。因此,将Padding属性设置为0将覆盖其设置为的值。选择项目时,不会调整组合框的大小
<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>
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;
}
}
}