简体   繁体   中英

Combobox selected item WPF

I have tried a code in which i am retrieving video names from folder. When i select some item from combobox it does not display the name in combobox selection. I go through all the stuff over stackoverflow. But none of it solved the issue. Any suggestions please.

Code

public partial class TextToSignWindow : Window
{
    public TextToSignWindow()
    {
        InitializeComponent();
        var rootFolder = System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        var root = rootFolder + @"\Videos\";
        string localPath = new Uri(root).LocalPath;
        PopulateListBox(data,localPath, "*.wmv");
    }

    private void PopulateListBox(ComboBox cmb, string Folder, string FileType)
    {
        DirectoryInfo dinfo = new DirectoryInfo(Folder);
        FileInfo[] Files = dinfo.GetFiles(FileType);
        foreach (FileInfo file in Files)
        {
            var ext = Path.GetExtension(file.Name);
            var name = Path.GetFileNameWithoutExtension(file.Name);
            cmb.Items.Add(name);
        }
    }

    private void data_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        data.SelectedValue =data.SelectedItem.ToString();
    }
}

WPF

<ComboBox x:Name="data" 
          IsEditable="True" 
          FontFamily="verdana" 
          IsSynchronizedWithCurrentItem="True" 
          FontSize="28" 
          Text="" 
          HorizontalAlignment="Left" 
          Height="81" 
          Margin="29,214,0,0" 
          VerticalAlignment="Top" 
          Width="326" 
          SelectionChanged="data_SelectionChanged_1" 
          SelectedItem="{Binding SelectedItem}" />

You need to include the DisplayMemberPath

<ComboBox x:Name="data" IsEditable="True" FontFamily="verdana" IsSynchronizedWithCurrentItem="True"  FontSize="28" Text=""  HorizontalAlignment="Left" Height="81" Margin="29,214,0,0" VerticalAlignment="Top" Width="326" SelectionChanged="data_SelectionChanged_1" SelectedItem="{Binding SelectedItem}" DisplayMemberPath="NameOfFieldToDisplay"/>

You may also need to include the SelectedValuePath="NameOfSelectedValueField"

For example: Typically the DisplayMemberPath is your Description string, and the SelectedValuePath is your Id int.

Here's a copy paste from code i'm currently using:

<ComboBox x:Name="comboBoxName" ItemsSource="{Binding ItemsList}" SelectedValuePath="Id" DisplayMemberPath="DisplayName" Width="300"  FontSize="14.667" SelectionChanged="comboBoxName_SelectionChanged">

I tried with a sample It worked

Xaml

 <ComboBox x:Name="data" IsEditable="True" FontFamily="verdana" IsSynchronizedWithCurrentItem="True"  FontSize="28" Text=""  HorizontalAlignment="Left" Height="81" Margin="29,214,0,0" VerticalAlignment="Top" Width="326" SelectionChanged="data_SelectionChanged_1"  />

Code Behind

 private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            data.Items.Add("test");
            data.Items.Add("test1");
        }   

        private void data_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            data.SelectedValue = data.SelectedItem.ToString();
        }

Dont send your combobox to function

  private void PopulateListBox(string Folder, string FileType)
{
    DirectoryInfo dinfo = new DirectoryInfo(Folder);
    FileInfo[] Files = dinfo.GetFiles(FileType);
    foreach (FileInfo file in Files)
    {
        var ext = Path.GetExtension(file.Name);
        var name = Path.GetFileNameWithoutExtension(file.Name);
        data.Items.Add(name);
    }
}

And if you wanna send, try it by ref

 private void PopulateListBox(ref ComboBox cmb, string Folder, string FileType)
    {
        DirectoryInfo dinfo = new DirectoryInfo(Folder);
        FileInfo[] Files = dinfo.GetFiles(FileType);
        foreach (FileInfo file in Files)
        {
            var ext = Path.GetExtension(file.Name);
            var name = Path.GetFileNameWithoutExtension(file.Name);
            cmb.Items.Add(name);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM