Home > front end >  Why SelectedValue no work in ComboBox with DataTemplate?
Why SelectedValue no work in ComboBox with DataTemplate?

Time:10-19

Here is my XAML:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <ComboBox x:Name="CB" ItemsSource="{Binding OC}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding ItemName}"></TextBlock>
                        <TextBlock Text="{Binding FileName}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <Button Click="Button_Click" Content="Try"></Button>
    </StackPanel>
</Window>

And here is the code-behind:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<ObjectItem> OC { get; set; } = new ObservableCollection<ObjectItem>();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            OC.Add(new ObjectItem() { ItemName = "123", FileName = "abc.txt" });
            OC.Add(new ObjectItem() { ItemName = "456", FileName = "cde.txt" });
            OC.Add(new ObjectItem() { ItemName = "789", FileName = "efg.txt" });            
        }
        public class ObjectItem { 
            public string ItemName { get; set; }
            public string FileName { get; set; }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CB.SelectedValue = new ObjectItem() { ItemName = "000", FileName = "ooo.txt" };
        }
    }
}

When I click the button, I want the ComboBox to display the Content I set without adding any item to the ObservableCollection, just like this: enter image description here

However, it doesn't work any while I click the button. What's wrong with it?

CodePudding user response:

The SelectedValue is per definition the SelectedValue in an ObservableCollection. You'll have to add it to the collection.

  •  Tags:  
  • wpf
  • Related