winui-3可编辑组合obox在应用程序开始时未显示绑定的文本
在应用程序开始时,使用有效的文本属性的Combobox错误地显示了占位符文本,而不是绑定的文本值 - 直到用户单击ComboBox。 点击导致正确的绑定文本显示并保持显示。链接到同一属性的相关文本框始终显示边界值。
如果选择了组合列表项目,则按预期工作,表明绑定是正确的。 它看起来像默认模板:(c:\ program文件(x86)\ Windows kits \ 10 \ distionTime \ commonConfiguration \ netral \ uap \ uap \ uap \ 10.0.18362.0 \ generic.xaml) 可能是不正确的,因为文本应显示一个值时,而不是占位符文本。如果这是原因,那么我还没有正确地重新定义视觉状态的技能,并将从提示中受益。
任何人都可以指向正确的方向,以便我可以在初始启动时显示文本属性,而无需单击。 使用的用户控制代码是:
用winui3 usercontrol包含文本框和组合框 - 每个绑定到同一属性的文本以两种方式模式进行演示,因此每个属性都会随着属性更改而更新。
使用当前使用CommunityToolKit 7.1.2的Nuget软件包 。
<UserControl
x:Class="mvvmTry1.Views.ComboBoxTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:mvvmTry1.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" Spacing="20" Background="AntiqueWhite">
<TextBox Header="Current Value" Text="{x:Bind MyText,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MinWidth="60"
Height="Auto" />
<ComboBox
Header=" ComboBox"
IsEditable="True"
ItemsSource="{x:Bind MyListItems, Mode=TwoWay}"
MinWidth="100"
PlaceholderText="PlaceHolder Text"
PlaceholderForeground="Green"
Text="{x:Bind MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</StackPanel>
</UserControl>
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Controls;
using System.Collections.ObjectModel;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace mvvmTry1.Views
{
[ObservableObject]
public partial class ComboBoxTest : UserControl
{
[ObservableProperty]
private string myText ="MyText value";
public ObservableCollection<string> MyListItems = new();
public ComboBoxTest()
{
this.InitializeComponent();
MyListItems.Add("Item 1");
MyListItems.Add("Item 2");
MyListItems.Add("Item 3");
}
}
}
On start of application the ComboBox with a valid Text Property incorrectly shows a placeholder text rather than the bound text value - until the user clicks the ComboBox.
The click causes the correct bound Text to show and stay shown. The related TextBox linked to same property correctly shows the bound value at all times.
If a Combo List item is selected it works as expected indicating Binding is correct.
It looks like the default template: (generic.xaml from C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.18362.0\Generic.xaml)
is likely incorrect as the Text should show when a value exists, rather than the Placeholder text. If this is the cause, then I do not yet have the skill to redefine the visual states correctly and would benefit from a hint.
Can anyone point me in the right direction so I can show the Text property at initial start without a click required.
The user control code used is:
Demonstrated with a WinUI3 usercontrol containing TextBox and a ComboBox - Text of each bound to the same property in two way mode so each will update with property changed.
Using current nuget package of CommunityToolkit 7.1.2 which uses
.net5.0-windows10.0.18362
Xaml Code ComboBoxTest.xaml:
<UserControl
x:Class="mvvmTry1.Views.ComboBoxTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:mvvmTry1.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" Spacing="20" Background="AntiqueWhite">
<TextBox Header="Current Value" Text="{x:Bind MyText,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MinWidth="60"
Height="Auto" />
<ComboBox
Header=" ComboBox"
IsEditable="True"
ItemsSource="{x:Bind MyListItems, Mode=TwoWay}"
MinWidth="100"
PlaceholderText="PlaceHolder Text"
PlaceholderForeground="Green"
Text="{x:Bind MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</StackPanel>
</UserControl>
CodeBehind ComboBoxTest.xaml.cs:
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Controls;
using System.Collections.ObjectModel;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace mvvmTry1.Views
{
[ObservableObject]
public partial class ComboBoxTest : UserControl
{
[ObservableProperty]
private string myText ="MyText value";
public ObservableCollection<string> MyListItems = new();
public ComboBoxTest()
{
this.InitializeComponent();
MyListItems.Add("Item 1");
MyListItems.Add("Item 2");
MyListItems.Add("Item 3");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论