加载表单时从 EntityModel WPF 中选择的 ComboBox 值
Selected ComboBox value from EntityModel WPF when form loaded
当我 运行 我的项目时,Combobox 没有 select 任何项目。但是如果我在将 Messagebox 加载到 Combobox 之前创建 Messagebox,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);
}
}
如果你发现错误,比通过代码突出显示要好得多。
谢谢
*please try this
<telerik:RadComboBox Grid.Row="5" Grid.Column="1"
x:Name="SiteAddressWatermark"
IsEditable="True" DisplayMemberPath="SiteAddress"
SelectedValuePath="Id" IsReadOnly="False"/>
using (var db = new ShareGateEntities())
{
var collections = db.Connections.ToList();
SiteAddressWatermark.ItemsSource = collections;
}*
当我 运行 我的项目时,Combobox 没有 select 任何项目。但是如果我在将 Messagebox 加载到 Combobox 之前创建 Messagebox,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);
}
}
如果你发现错误,比通过代码突出显示要好得多。
谢谢
*please try this
<telerik:RadComboBox Grid.Row="5" Grid.Column="1"
x:Name="SiteAddressWatermark"
IsEditable="True" DisplayMemberPath="SiteAddress"
SelectedValuePath="Id" IsReadOnly="False"/>
using (var db = new ShareGateEntities())
{
var collections = db.Connections.ToList();
SiteAddressWatermark.ItemsSource = collections;
}*