是否有机会更改仅在单击左侧箭头时打开的wpf组合框? 通常,您可以单击任意位置将其打开。我不要。
谢谢
答案 0 :(得分:3)
通常,您可以单击任意位置将其打开。我不要。
然后,您应该为ToggleButton
创建一个自定义模板。 Yan在Visual Studio或Blend中的设计模式下右键单击ComboBox
元素,然后选择“编辑模板”->“编辑副本”。
这会将默认模板复制到您的XAML标记中,然后您可以根据需要对其进行编辑。寻找一个Style
和一个x:Key
的“ ComboBoxToggleButton”,然后修改这个ControlTemplate
。
答案 1 :(得分:2)
我设法使其与以下代码一起使用。请注意,我正在使用Materialdesign,如果不使用它,则必须对其进行一些更改。
App.xaml
中的代码
<Style x:Key="MaterialDesignComboBox2" TargetType="{x:Type ComboBox}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignTextBoxBorder}"/>
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}"/>
<Setter Property="BorderThickness" Value="0 0 0 1"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource MaterialDesignComboBoxItemSelectedCollapsedStyle}" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="Padding" Value="0 6 0 6" />
<Setter Property="VerticalContentAlignment" Value="Top" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
<Setter Property="Stylus.IsFlicksEnabled" Value="False" />
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource MaterialDesignValidationErrorTemplate}"/>
<Setter Property="wpf:TextFieldAssist.TextBoxViewMargin" Value="1 0 1 0" />
<Setter Property="Template" Value="{StaticResource MaterialDesignFloatingHintComboBoxTemplate2}" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}" />
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}" />
</Trigger>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false" />
<!-- designer prefers hard bool -->
<Setter Property="wpf:ComboBoxAssist.ShowSelectedItem" Value="{StaticResource TrueValue}" />
</Trigger>
<!-- designer prefers hard bool -->
<Trigger Property="wpf:ComboBoxAssist.ShowSelectedItem" Value="{StaticResource TrueValue}" >
<Setter Property="ItemContainerStyle" Value="{StaticResource MaterialDesignComboBoxItemStyle}" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="MaterialDesignFloatingHintComboBox2"
BasedOn="{StaticResource MaterialDesignComboBox2}"
TargetType="{x:Type ComboBox}">
<Setter Property="wpf:HintAssist.IsFloating" Value="True"/>
</Style>
Helperclass EditableComboBox
public static class EditableComboBox
{
public static int GetMaxLength(DependencyObject obj)
{
return (int)obj.GetValue(MaxLengthProperty);
}
public static void SetMaxLength(DependencyObject obj, int value)
{
obj.SetValue(MaxLengthProperty, value);
}
public static readonly DependencyProperty MaxLengthProperty = DependencyProperty.RegisterAttached("MaxLength", typeof(int), typeof(EditableComboBox), new UIPropertyMetadata(OnMaxLenghtChanged));
private static void OnMaxLenghtChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
if (!(obj is ComboBox comboBox)) return;
comboBox.Loaded +=
(s, e) =>
{
var textBox = comboBox.FindChild("PART_EditableTextBox", typeof(TextBox));
if (textBox == null) return;
textBox.SetValue(TextBox.MaxLengthProperty, args.NewValue);
};
}
}
以及视图中的组合框本身
<ComboBox materialDesign:HintAssist.Hint="{lex:Loc ordernumber}" Style="{StaticResource MaterialDesignFloatingHintComboBox2}" Text="{Binding SalesOrderNumber}" Margin="0 0 40 0" FontSize="22" Width="250"
IsEditable="True" DisplayMemberPath="Identifier" SelectedItem="{Binding SelectedSalesOrder}"
ItemsSource="{Binding LastSalesOrders}" IsTextSearchEnabled="False" utility:EditableComboBox.MaxLength="10" x:Name="TbOrder" >
<ComboBox.InputBindings>
<KeyBinding Command="{Binding OpenSalesOrderOrCustomerCommand}" Key="Return" />
</ComboBox.InputBindings>
<ComboBox.ContextMenu>
<ContextMenu>
<MenuItem Header="{lex:Loc paste}" Command="{Binding PasteOrdernumberCommand}" />
</ContextMenu>
</ComboBox.ContextMenu>
</ComboBox>