我的WPF应用程序中有一个组合框:
<ComboBox ItemsSource="{Binding CompetitorBrands}" DisplayMemberPath="Value"
SelectedValuePath="Key" SelectedValue="{Binding Path=CompMfgBrandID, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" Text="{Binding CompMFGText}"/>
Run Code Online (Sandbox Code Playgroud)
绑定到的集合 KeyValuePair<string, string>
这是我的ViewModel中的CompMfgBrandID属性:
public string CompMfgBrandID
{
get { return _compMFG; }
set
{
if (StockToExchange != null && StockToExchange.Where(x => !string.IsNullOrEmpty(x.EnteredPartNumber)).Count() > 0)
{
var dr = MessageBox.Show("Changing the competitor manufacturer will remove all entered parts from the transaction. Proceed?",
"Transaction Type", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr != DialogResult.Yes)
return;
}
_compMFG = value;
StockToExchange.Clear();
...a bunch of other functions that don't get called when you click …Run Code Online (Sandbox Code Playgroud) 我想在WPF中创建一个null顶部有一个项目的ComboBox,当选中此项时,SelectedItem应设置为null(重置为默认状态).我一直在搜索,但没有找到令人满意的解决方案.
如果可能的话,我希望它只使用XAML代码或附加行为,因为我不喜欢在ViewModel中更改View或覆盖标准控件.
这是我到目前为止提出的(缩短代码):
[...]
<Popup x:Name="PART_Popup" [...]>
<Border x:Name="PopupBorder" [...]>
<ScrollViewer x:Name="DropDownScrollViewer" [...]>
<StackPanel [...]>
<ComboBoxItem>(None)</ComboBoxItem>
<ItemsPresenter x:Name="ItemsPresenter"/>
</StackPanel>
</ScrollViewer>
</Border>
</Popup>
[...]
Run Code Online (Sandbox Code Playgroud)

我认为最好的办法就是以某种方式添加一个事件触发器,设置SelectedIndex到-1当项目被选中,但这里是我卡住了.
任何想法如何做到这一点?或者更好的方式,如附加行为?