获取带有文本块的组合框的值

时间:2017-12-09 12:21:08

标签: c# wpf xaml combobox

我有一个蛇形颜色的组合框,我想知道用户选择了哪种模式,我添加了图片来显示可用的模式,但是当我做的时候 图案= combobox2.Text;它没有向我显示我正在寻找的文本。

<ComboBox Name="ComboBox2" FontWeight="Bold" FontSize="15" Canvas.Left="320" Canvas.Top="222" Width="135" Height="24">
        <ComboBoxItem  
         Name="Pattern1" FontFamily="Verdana" FontSize="12" FontWeight="Bold">
            <StackPanel Orientation="Horizontal">
                <Image Source="Pics/snakepattern.jpg" Height="12" Width="19"></Image>
                <TextBlock Text=" Pattern1"></TextBlock>
            </StackPanel>
        </ComboBoxItem>
        <ComboBoxItem
        Name="Pattern2" FontFamily="Verdana" FontSize="12" FontWeight="Bold">
            <StackPanel Orientation="Horizontal">
                <Image Source="Pics/snakepattern2.jpg" Height="14" Width="20"></Image>
                <TextBlock Text=" Pattern2"></TextBlock>
            </StackPanel>
        </ComboBoxItem>
        <ComboBoxItem  
        Name="Pattern3" FontFamily="Verdana" FontSize="12" FontWeight="Bold">
            <StackPanel Orientation="Horizontal" Height="15">
                <Image Source="Pics/snakepattern3.jpg" Height="20"></Image>
                <TextBlock Text=" Pattern3"></TextBlock>
            </StackPanel>
        </ComboBoxItem>
        <ComboBoxItem 
        Name="Pattern4" FontFamily="Verdana" FontSize="12" FontWeight="Bold">
            <StackPanel Orientation="Horizontal">
                <Image Source="Pics/snakepattern4.jpg" Height="13" Width="20"></Image>
                <TextBlock Text=" Pattern4"></TextBlock>
            </StackPanel>
        </ComboBoxItem>
        <ComboBoxItem 
        Name="Pattern5" FontFamily="Verdana" FontSize="12" FontWeight="Bold">
            <StackPanel Orientation="Horizontal">
                <Image Source="Pics/snakepattern5.png" Height="17" Width="23"></Image>
                <TextBlock Text=" Pattern5"></TextBlock>
            </StackPanel>
        </ComboBoxItem>
    </ComboBox>

我想让它向我展示文字“Pattern1”,“Pattern2”(根据用户选择的内容) 我如何在c#代码中执行此操作?

1 个答案:

答案 0 :(得分:1)

你应该有这样的视图模型:

public class SnakePattern
{
    public string Name { get; set; }
    public ImageSource Image { get; set; }
}

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<SnakePattern> Patterns { get; }
        = new ObservableCollection<SnakePattern>();

    private SnakePattern selectedPattern;
    public SnakePattern SelectedPattern
    {
        get { return selectedPattern; }
        set
        {
            selectedPattern = value;
            PropertyChanged?.Invoke(this,
                new PropertyChangedEventArgs(nameof(SelectedPattern)));
        }
    }
}

并将ComboBox绑定到它:

<ComboBox ItemsSource="{Binding Patterns}"
          SelectedItem="{Binding SelectedPattern}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Image}" Height="12" Width="19"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

在MainWindow的构造函数中初始化它,并将其分配给MainWindow DataContext属性,如下所示:

public MainWindow()
{
    InitializeComponent();

    var vm = new ViewModel();
    vm.Patterns.Add(new SnakePattern
    {
        Name = "Pattern 1",
        Image = new BitmapImage(new Uri("pack://application:,,,/Pics/snakepattern1.jpg"))
    });
    ...
    vm.SelectedPattern = vm.Patterns[0];

    DataContext = vm;
}

要获取所选模式,只需访问DataContext(或将视图模型保留在成员变量中):

var vm = (ViewModel)DataContext;
var selectedPattern = vm.SelectedPattern;