我想在保存组合框中的项目时更新该项目的文本。
虽然我使用作为源的实体实现了notify属性更改事件,但它不是更新。我已经阅读了解决方案,它是使用数据模板而不是DisplayMemberPath。
所以我用这个:
<DataTemplate x:Key="TipoComponenteTemplate"
DataType="{x:Type clases:TiposComponentesUI}">
<TextBlock Text="{Binding TipoComponente}"/>
</DataTemplate>
<ComboBox Name="cmbTiposComponentes" Width="150"
Text="{Binding TiposComponentesTexto, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding TiposComponentes}"
ItemTemplate="{StaticResource ResourceKey=TipoComponenteTemplate}"
SelectedItem="{Binding TiposComponentesSelectedItem}">
这部分工作,当我打开下拉列表框,显示正确的文本,但当我选择其中一个,在组合框的文本框显示类的名称,而不是财产TipoComponente中的数据在数据定义模板。
所以我想知道如何在文本框中显示与项目中显示的文本相同的文本。
谢谢。
我已经复制了这个场景,ItemTemplate
和DisplayMemberPath
解决方案对我来说都很好,只要ComboBox
的IsEditable
属性设置为False
。
IsEditable="False"
我有:
Book.cs
using CommunityToolkit.Mvvm.ComponentModel;
namespace WpfApp2;
public partial class Book : ObservableObject
{
[ObservableProperty]
private string? _author;
[ObservableProperty]
private string? _title;
}
MainViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.ObjectModel;
namespace WpfApp2;
public partial class MainViewModel : ObservableObject
{
public ObservableCollection<Book> Books { get; } = new();
[ObservableProperty]
private Book? _selectedBook;
public MainViewModel()
{
Books.Add(new Book { Author = "Jim Weasley", Title = "The mystery of eggplants" });
Books.Add(new Book { Author = "Ryan Reynolds", Title = "Doplhins and how to beat them" });
Books.Add(new Book { Author = "Sarah O'Connel", Title = "What is djent?" });
}
[RelayCommand]
private void UpdateRandomBookTitles()
{
foreach (var book in Books)
{
book.Title = Guid.NewGuid().ToString();
}
}
}
MainWindow.xaml.cs
<Window
x:Class="WpfApp2.MainWindow"
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:local="clr-namespace:WpfApp2"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="BookDisplayTemplate" DataType="{x:Type local:Book}">
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<Button Command="{Binding UpdateRandomBookTitlesCommand}" Content="Randomize book titles" />
<ComboBox
ItemTemplate="{StaticResource BookDisplayTemplate}"
ItemsSource="{Binding Books}"
SelectedItem="{Binding SelectedBook}" />
<!-- This will work as well -->
<!-- <ComboBox
DisplayMemberPath="Title"
ItemsSource="{Binding Books}"
SelectedItem="{Binding SelectedBook}" />-->
</StackPanel>
</Window>
在你选择了一本书之后,改变它的Title
也会反映在ComboBox
的Text
中。
问题:IsEditable="True"
一旦你设置了IsEditable="True"
,这个停止工作,我相信这是故意的。当IsEditable="True"
时,ComboBox
的Text
仅在以下情况下更新:
- 一个项目被选中
- 用户手动输入文本
更改SelectedBook
的Title
在这里没有任何作用,我的猜测是这样它就不会覆盖用户输入。