【发布时间】:2022-01-14 04:45:44
【问题描述】:
我正在寻找将组合框中的第一项设置为默认值的可能性。
我对组合框有自己的看法 (XAML):
<ComboBox ItemsSource="{Binding SiteScripts, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedValue="{Binding SelectedSiteScriptId}"
SelectedIndex="0" SelectedValuePath="Id"
DisplayMemberPath="Name" Background="AliceBlue"
Margin="5,10,10,540" Width="100" Height="50"/>
我从组合框的ItemsSource 属性中的视图模型绑定到我的列表,并且我的SelectedValue 绑定到ID 属性项:
public List<SiteCreateMachineScripts> SiteScripts
{
get { return _siteScripts; }
set
{
_siteScripts = value;
NotifyOfPropertyChange();
}
}
public int SelectedSiteScriptId
{
get { return _selectedSiteScriptId; }
set
{
_selectedSiteScriptId = value;
NotifyOfPropertyChange();
}
}
我已经尝试设置SelectedIndex = 0,但没有成功
感谢您的建议:)
【问题讨论】:
-
与其改变控件属性,不如改变它绑定的viewmodel属性。
标签: c# wpf xaml combobox selecteditem