我正在製作一個用戶控件,其中包含一個按鈕和一個組合框。組合框是隻讀的。我定義了一個DataTemplate
,組合框中的ItemsSource
設置爲一個雙精度列表,它等於我在用戶控件中定義爲依賴屬性的值的百分比。我的意圖是顯示每個項目,用戶控件中的值的百分比。C#/ wpf組合框模板項目高度
一切工作正常,但組合框的下拉打開時項目的高度大約是組合框本身高度的兩倍,當我選擇一個時,它將用戶控件的高度設置爲大小的組合框。
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;
}
}
}