WPF如何获取静态下拉框选中的ComboBoxItem的text文本值

xaml部分:

<ComboBox Margin="10" Padding="5" BorderBrush="LightGray" BorderThickness="1" FontSize="15" SelectedItem="{Binding Shuttle1Value}">
    <ComboBoxItem IsSelected="True">请选择</ComboBoxItem>
    <ComboBoxItem>去等待位</ComboBoxItem>
    <ComboBoxItem>去接驳位</ComboBoxItem>
    <ComboBoxItem>入库</ComboBoxItem>
    <ComboBoxItem>出库</ComboBoxItem>
    <ComboBoxItem>停止</ComboBoxItem>
    <ComboBoxItem>复位</ComboBoxItem>
    <ComboBoxItem>复位报警后清状态</ComboBoxItem>
    <ComboBoxItem>充电</ComboBoxItem>
    <ComboBoxItem>更新位置</ComboBoxItem>
</ComboBox>

<Button Command="{Binding TriggerCommand}" Content="下发" />

c#代码(viewmodel)部分:

 

 public class DeviceManageViewModel : BindableBase
 {
public DelegateCommand TriggerCommand {  get; private set; }
    private ComboBoxItem shuttle1Value;
     public ComboBoxItem Shuttle1Value
     {
         get { return shuttle1Value; }
         set { shuttle1Value = value; RaisePropertyChanged(); }
     }        


 public DeviceManageViewModel()
 {
    
     TriggerCommand = new DelegateCommand(Shuttle1Trigger);
}

      public void Shuttle1Trigger()
      {
          var y=Shuttle1Value.Content;
          MessageBox.Show(y.ToString());
      }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过设置ComboBoxItemContainerStyle属性来实现下拉框选中行的背景颜色。具体步骤如下: 1. 在ComboBox中添加ItemContainerStyle属性,指定一个Style,如下所示: ``` <ComboBox ItemContainerStyle="{StaticResource ComboBoxItemStyle}"> <ComboBoxItem>选项1</ComboBoxItem> <ComboBoxItem>选项2</ComboBoxItem> <ComboBoxItem>选项3</ComboBoxItem> </ComboBox> ``` 2. 在Window或者Application的Resources中定义一个Style,名为ComboBoxItemStyle,如下所示: ``` <Window.Resources> <Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ComboBoxItem}"> <Border Name="Border" Padding="2" SnapsToDevicePixels="true" Background="{TemplateBinding Background}"> <ContentPresenter /> </Border> <ControlTemplate.Triggers> <Trigger Property="IsHighlighted" Value="true"> <Setter TargetName="Border" Property="Background" Value="LightBlue" /> </Trigger> <Trigger Property="IsSelected" Value="true"> <Setter TargetName="Border" Property="Background" Value="LightGreen" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> ``` 其中,IsHighlighted和IsSelected分别表示鼠标悬停和被选中。在上述代码中,当IsHighlighted为true时,设置选项的背景为LightBlue;当IsSelected为true时,设置选项的背景为LightGreen。 通过以上步骤,就可以实现ComboBox下拉框选中行的背景颜色了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值