CommandParameter参数绑定之Combobox的下拉选项

准备工作

public class CutEntity
{
    public int Id { get; set; }

    public string CutType { get; set; }
    public string EdgeSide { get; set; }
    public double X { get; set; }
    public double Y { get; set; }

    public CutEntity(int id, string cutType, string edgeSide, double x, double y)
    {
        Id = id;
        CutType = cutType;
        EdgeSide = edgeSide;
        X = x;
        Y = y;
    }
}
 private ObservableCollection<CutEntity> _cutEntitys;

 public ObservableCollection<CutEntity> CutEntitys
 {
     get => _cutEntitys ?? (_cutEntitys = new ObservableCollection<CutEntity>());
     set => SetProperty(ref _cutEntitys, value);
 }
  <ComboBox
     x:Name="com"
     Width="200"
     Height="30"
     DisplayMemberPath="EdgeSide"
     ItemsSource="{Binding CutEntitys}"
     SelectedValue="{Binding SelectedValue}"
     SelectedValuePath="EdgeSide"
     Text="{Binding Text}" />

1、使用Text绑定

 <Button
     Width="100"
     Height="30"
     Margin="5"
     Command="{Binding OpenCommand}"
     CommandParameter="{Binding Text}"
     Content="Open1" />

2、使用SelectedValue结合SelectedValuePath

 <Button
     Width="100"
     Height="30"
     Margin="5"
     Command="{Binding OpenCommand}"
     CommandParameter="{Binding SelectedValue}"
     Content="Open2" />

3、使用SelectedItem.Property

 <Button
     Width="100"
     Height="30"
     Margin="5"
     Command="{Binding OpenCommand}"
     CommandParameter="{Binding SelectedItem.EdgeSide, ElementName=com}"
     Content="Open3" />
posted @ 2025-07-04 23:08  孤沉  阅读(9)  评论(0)    收藏  举报