簡體   English   中英

組合框選擇的項目WPF

[英]Combobox selected item WPF

我嘗試了一個代碼,在其中我從文件夾中檢索視頻名稱。 當我從組合框選擇某些項目時,它不會在組合框選擇中顯示名稱。 我遍歷了stackoverflow的所有內容。 但是,沒有一個解決問題。 有任何建議請。

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}" />

您需要包括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"/>

您可能還需要包括SelectedValuePath =“ NameOfSelectedValueField”

例如:通常,DisplayMemberPath是您的Description字符串,而SelectedValuePath是您的ID整數。

這是我當前使用的代碼的復制粘貼:

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

我嘗試了一個樣本

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"  />

背后的代碼

 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();
        }

不要讓您的組合框起作用

  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);
    }
}

如果您想發送,請通過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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM