C# 组合框选定项WPF
我尝试了一个代码,其中我正在从文件夹中检索视频名称。当我从combobox中选择某个项目时,它不会在combobox选择中显示名称。我把所有的东西都翻了一遍。但这一切都没有解决问题。有什么建议吗 代码C# 组合框选定项WPF,c#,wpf,combobox,C#,Wpf,Combobox,我尝试了一个代码,其中我正在从文件夹中检索视频名称。当我从combobox中选择某个项目时,它不会在combobox选择中显示名称。我把所有的东西都翻了一遍。但这一切都没有解决问题。有什么建议吗 代码 public partial class TextToSignWindow : Window { public TextToSignWindow() { InitializeComponent(); var rootFolder = System.I
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是您的描述字符串,SelectedValuePath是您的Id int
下面是我当前使用的代码的复制粘贴:
<ComboBox x:Name="comboBoxName" ItemsSource="{Binding ItemsList}" SelectedValuePath="Id" DisplayMemberPath="DisplayName" Width="300" FontSize="14.667" SelectionChanged="comboBoxName_SelectionChanged">
我试过一个样本,它很有效 Xaml
不要将组合框发送到功能
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);
}
}
试试这个:cmd.Items.add(new{FileName=name});REMOVE:SelectedItem=“{Binding SelectedItem}”ADD:DisplayMemberPath=“FileName”SelectedValuePath=“FileName”@Kris它在
data.SelectedValue=data.SelectedItem.ToString()处传递异常;data.SelectedValue=selectedItem.FileName@Kris仍然不显示name is selected item当SelectionChanged事件触发时,不应将所选值设置为自身。动态selectedItem=data.selectedItem;var SelectedValue=selectedItem.FileName;
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);
}
}
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);
}
}