C# 选择特定ComboBoxItem时,如何将可编辑ComboxBox的文本更改为其他文本?
新文本将永远不会出现在组合框内的项目中。下面是完整的XAML+代码,它不能像我希望的那样工作。我试图实现的是让用户从组合框中选择一组实际的项目,或者选择一个无组斜体、灰显的项目,该项目应作为组合框内文本框中的空字符串应用 我还尝试: with StaysOpenOnEdit=True,结果相同,且 通过处理click me ComboxItem的选定事件,在更改ComboxBox的文本或SelectedItem属性之前调用事件处理程序。 XAML 预期:当用户单击下拉列表中的“单击我”项目时,组合框的文本将变为空字符串。单击其他项时,应将其文本正常复制到组合框的文本框中 实际: 启动程序。 选择“单击我”项目。 文本更改为“单击我”而不是灰显。 单击测试项。 文本变为空字符串,而不是test。 再次单击测试项。 文本更改为test。 使现代化 我希望使用MVVM,但我还是一个初学者。我在DataGridTemplate列中有几个组合框,如上图所示,对于每个具有相同下拉内容的组合框,我认为每个组合框项都应该有一个ViewModel。如果可能的话,我想学习如何在这种情况下正确使用MVVM 大XAMLC# 选择特定ComboBoxItem时,如何将可编辑ComboxBox的文本更改为其他文本?,c#,wpf,xaml,combobox,selectionchanged,C#,Wpf,Xaml,Combobox,Selectionchanged,新文本将永远不会出现在组合框内的项目中。下面是完整的XAML+代码,它不能像我希望的那样工作。我试图实现的是让用户从组合框中选择一组实际的项目,或者选择一个无组斜体、灰显的项目,该项目应作为组合框内文本框中的空字符串应用 我还尝试: with StaysOpenOnEdit=True,结果相同,且 通过处理click me ComboxItem的选定事件,在更改ComboxBox的文本或SelectedItem属性之前调用事件处理程序。 XAML 预期:当用户单击下拉列表中的“单击我”项目时,组
组合框。文本不会在SelectionChanged事件中立即更新。您可以改为使用SelectedValue的内容。并将SelectedValue设置为null。像这样改变你的if条件
if ((cb.SelectedValue as ComboBoxItem).Content.ToString() == "click me")
{
cb.Text = "";
cb.SelectedValue = null;
e.Handled = true;
}
您正在尝试使用WPF以前从未设计过的老方法。这就是使用winforms的方式。我强烈建议将来使用MVVM和数据绑定,因为这将以一种更优雅的方式解决类似的问题,这也是WPF设计的设计模式。但是,如果您坚持这样做,请使用调试器查看cb.Text是什么,当您检查它时,您将看到它还没有单击我我必须将SelectedValue设置为null的原因是什么?如果在这种情况下我使用SelectedItem而不是SelectedValue,可以吗?了解SelectedValue和SelectedItem在这里@silviubogan您可以将其设置为Answer,因为它解决了您的问题。这样才能帮助其他面临同样问题的人。
internal bool HandlingSelectionChange = false;
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (HandlingSelectionChange)
{
return;
}
HandlingSelectionChange = true;
var cb = sender as ComboBox;
if (cb.Text == "click me")
{
cb.Text = "";
e.Handled = true;
}
HandlingSelectionChange = false;
}
<DataGridTemplateColumn Header="Group Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding GroupName, Mode=OneWay}">
</Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox IsEditable="True" StaysOpenOnEdit="True"
ItemsSource="{Binding Path=Clocks.GroupsVM,
RelativeSource={RelativeSource AncestorType=local:ClockDataGrid}}"
PreviewKeyDown="ComboBox_PreviewKeyDown"
SelectionChanged="ComboBox_SelectionChanged"
Text="{Binding GroupName}">
<ComboBox.Resources>
<Style TargetType="ComboBoxItem">
<Setter Property="FontStyle" Value="{Binding FontStyle}"/>
<Setter Property="Foreground" Value="{Binding Foreground}"/>
</Style>
</ComboBox.Resources>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
private void ComboBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
var cb = sender as ComboBox;
if ((e.Key == Key.Return ||
e.Key == Key.Enter) &&
cb.Text != "")
{
bool duplicate = false;
foreach (ClockGroupVM vm in Clocks.GroupsVM)
{
if (vm.Name == cb.Text)
{
cb.SelectedItem = vm;
duplicate = true;
break;
}
}
if (duplicate)
{
return;
}
// create a ClockGroupM and corresponding ClockGroupVM
// (ClockGroupVM inherits from ClockGroupM)
var cvm = new ClockGroupVM()
{
Name = cb.Text
};
Clocks.Groups.Insert(0, cvm);
cb.SelectedItem = cvm;
}
}
internal bool HandlingSelectionChange = false;
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (HandlingSelectionChange)
{
return;
}
HandlingSelectionChange = true;
var cb = sender as ComboBox;
//if (cb.SelectedItem is the VM with Style != Normal)
ClockGroupVM foundVM = null;
foreach (ClockGroupVM vm in Clocks.GroupsVM)
{
if (vm.FontStyle != FontStyles.Normal &&
ReferenceEquals(cb.SelectedItem, vm))
{
foundVM = vm;
break;
}
}
if (foundVM != null)
{
cb.Text = "";
e.Handled = true;
}
HandlingSelectionChange = false;
}
if ((cb.SelectedValue as ComboBoxItem).Content.ToString() == "click me")
{
cb.Text = "";
cb.SelectedValue = null;
e.Handled = true;
}