【发布时间】:2018-12-06 08:48:35
【问题描述】:
当我运行我的项目时,Combobox 不会选择任何项目。但是如果我在将 Messagebox 加载到 Combobox 之前创建它,MessageBox 会显示值。 请帮帮我:
我的 XAML 代码:
<Window x:Class="WpfApp1.MainWindow"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
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:WpfApp1"
mc:Ignorable="d"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="{DynamicResource MaterialDesignFont}"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<ComboBox x:Name="ComboboxTest" Background="White" HorizontalAlignment="Left" Margin="78,195,0,0" VerticalAlignment="Top" Width="139" IsEditable="True" IsTextSearchEnabled="True" IsEnabled="True" FlowDirection="RightToLeft" FontFamily="IRANSans" FontSize="14" TabIndex="1" Focusable="True" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
DisplayMemberPath="TitleNoeArz"
SelectedValuePath="IDNoeArz"
SelectedValue="{Binding IDNoeArz}" Height="32">
</ComboBox>
</Grid>
</Window>
还有我的 CS 代码
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
SBGharardadEntities sBGharardadEntities = new SBGharardadEntities();
ComboboxTest.ItemsSource = sBGharardadEntities.NoeArzs.ToList();
ComboboxTest.SelectedValue= 2;
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
}
如果你发现错误比通过代码突出显示会好得多。
谢谢
【问题讨论】:
标签: c# wpf combobox selectedvalue