将新记录插入 ObservableCollection,然后将值分配给 ComboBox SelectedValue 不显示
Inserting a new record into an ObservableCollection, then assigning a value to a ComboBox SelectedValue doesn't display
我在 WPF 应用程序中有一个 ComboBox,我通过查找 table 填充它。要求表明,如果用户已选择将新记录创建为数据类型,则 ComboBox 将显示文本“Select...”。该文本不在查找中 table,因此我创建了一个虚拟记录并将其插入用于填充 ComboBox 的 ObservableCollection,如下所示:
var newTmpCert = new CertificationType()
{
ID = 0,
CertType = SELECT_DROPDOWN_STRING, //SELECT_DROPDOWN_STRING == "Select..."
Inactive = false
};
CertTypeList.Insert(0, newTmpCert);
CertTypeList 定义如下:
private ObservableCollection<CertificationType> certTypeList;
public ObservableCollection<CertificationType> CertTypeList
{
get { return certTypeList; }
set
{
certTypeList = value;
RaisePropertyChanged();
}
}
我一直在努力让组合框显示“Select...”的任务是:
Course new_record = new Course();
new_record.CertificationLevel = "Re-certification";
Course = new_record;
CertificationTypeID = 0;
ComboBox 的 XAML 是这样的:
<ComboBox Width="200"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding CertTypeList}"
DisplayMemberPath="CertType"
SelectedValuePath="ID"
SelectedValue="{Binding CertificationTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
每次我测试应用程序并select创建新的认证记录时,组合框总是空白。所以,我想我会尝试一些不同的东西,并将 CertificationTypeID 分配给 2(该查找中有第三条记录 table),效果很好。
为什么有效? ObservableCollection 应该引发 propertychanged 事件。 ComboBox 未绑定到数据库中的查找 table,它绑定到 ObservableCollection。为什么它可以工作,如果我在查找中指定一个现有记录 table,但如果我在视图模型中将 0 分配给 CertificationTypeID 就不起作用?
如果您对 HappyNomad 解决方案感到满意,我认为这只是一个绑定问题。您是否检查了 Output-window 是否有任何来自 XAML 的错误消息(当 运行 程序时)。
我用那个解决方案做了一个例子,ContentControl XAML 看起来像这样:
<ContentControl Content="{Binding}">
<ContentControl.ContentTemplate>
<DataTemplate>
<Grid>
<ComboBox x:Name="cb" Width="200" HorizontalAlignment="Left"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding CertTypeList}"
DisplayMemberPath="CertType"
SelectedValuePath="ID"
SelectedValue="{Binding CertificationTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock x:Name="tb" Text="Select..." IsHitTestVisible="False" Visibility="Collapsed"/>
</Grid>
<DataTemplate.Triggers>
<Trigger SourceName="cb" Property="SelectedItem" Value="{x:Null}">
<Setter TargetName="tb" Property="Visibility" Value="Visible"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
我在 WPF 应用程序中有一个 ComboBox,我通过查找 table 填充它。要求表明,如果用户已选择将新记录创建为数据类型,则 ComboBox 将显示文本“Select...”。该文本不在查找中 table,因此我创建了一个虚拟记录并将其插入用于填充 ComboBox 的 ObservableCollection,如下所示:
var newTmpCert = new CertificationType()
{
ID = 0,
CertType = SELECT_DROPDOWN_STRING, //SELECT_DROPDOWN_STRING == "Select..."
Inactive = false
};
CertTypeList.Insert(0, newTmpCert);
CertTypeList 定义如下:
private ObservableCollection<CertificationType> certTypeList;
public ObservableCollection<CertificationType> CertTypeList
{
get { return certTypeList; }
set
{
certTypeList = value;
RaisePropertyChanged();
}
}
我一直在努力让组合框显示“Select...”的任务是:
Course new_record = new Course();
new_record.CertificationLevel = "Re-certification";
Course = new_record;
CertificationTypeID = 0;
ComboBox 的 XAML 是这样的:
<ComboBox Width="200"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding CertTypeList}"
DisplayMemberPath="CertType"
SelectedValuePath="ID"
SelectedValue="{Binding CertificationTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
每次我测试应用程序并select创建新的认证记录时,组合框总是空白。所以,我想我会尝试一些不同的东西,并将 CertificationTypeID 分配给 2(该查找中有第三条记录 table),效果很好。
为什么有效? ObservableCollection 应该引发 propertychanged 事件。 ComboBox 未绑定到数据库中的查找 table,它绑定到 ObservableCollection。为什么它可以工作,如果我在查找中指定一个现有记录 table,但如果我在视图模型中将 0 分配给 CertificationTypeID 就不起作用?
如果您对 HappyNomad 解决方案感到满意,我认为这只是一个绑定问题。您是否检查了 Output-window 是否有任何来自 XAML 的错误消息(当 运行 程序时)。 我用那个解决方案做了一个例子,ContentControl XAML 看起来像这样:
<ContentControl Content="{Binding}">
<ContentControl.ContentTemplate>
<DataTemplate>
<Grid>
<ComboBox x:Name="cb" Width="200" HorizontalAlignment="Left"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding CertTypeList}"
DisplayMemberPath="CertType"
SelectedValuePath="ID"
SelectedValue="{Binding CertificationTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock x:Name="tb" Text="Select..." IsHitTestVisible="False" Visibility="Collapsed"/>
</Grid>
<DataTemplate.Triggers>
<Trigger SourceName="cb" Property="SelectedItem" Value="{x:Null}">
<Setter TargetName="tb" Property="Visibility" Value="Visible"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>