WPF&x27_';属性绑定中的字符无效
在我的WPF应用程序的主窗口上,我有一个简单的列表框,它被数据绑定到一个ObservableCollection。ObservableCollection包含具有一个字符串属性的简单“Product”类的成员。目标是显示存储在ObservableCollection的所有产品的“PName”属性中的文本 MainWindow.xaml:WPF&x27_';属性绑定中的字符无效,wpf,binding,Wpf,Binding,在我的WPF应用程序的主窗口上,我有一个简单的列表框,它被数据绑定到一个ObservableCollection。ObservableCollection包含具有一个字符串属性的简单“Product”类的成员。目标是显示存储在ObservableCollection的所有产品的“PName”属性中的文本 MainWindow.xaml: <Window x:Class="BindingCheck.MainWindow" xmlns="http://schemas.microsoft
<Window x:Class="BindingCheck.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BindingCheck"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListBox Name="listBox1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label FontSize="26" Content="{Binding Path=PName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
MainWindow.Xaml.Cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace BindingCheck
{
public class Product
{
private string pName;
public string PName
{
get { return pName; }
}
public Product(string pname)
{
pName = pname;
}
}
public partial class MainWindow : Window
{
private ObservableCollection<Product> products;
public MainWindow()
{
InitializeComponent();
products = new ObservableCollection<Product>();
products.Add(new Product("Toaster"));
products.Add(new Product("Big_Toaster"));
products.Add(new Product("Very_Big_Toaster"));
this.DataContext = products;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows;
命名空间绑定检查
{
公共类产品
{
私有字符串pName;
公共字符串PName
{
获取{return pName;}
}
公共产品(字符串pname)
{
pName=pName;
}
}
公共部分类主窗口:窗口
{
私人可观测收集产品;
公共主窗口()
{
初始化组件();
产品=新的可观察收集();
添加(新产品(“烤面包机”);
添加(新产品(“Big_烤面包机”);
添加(新产品(“非常大的烤面包机”);
this.DataContext=产品;
}
}
}
现在我的问题是:为什么listBox输出中只显示每秒钟一个“\u1”字符?输出项应为:“Toaster”、“Big_Toaster”和“Very_Big_Toaster”,但我得到另一个输出:
参见以下代码:
<StackPanel>
<Label Content="_first_second" Target="{Binding ElementName=txtbox}"/>
<TextBox Name="txtbox" Width="100" Height="50"/>
</StackPanel>
现在,在加载了屏幕后
按任意“f”键,您将看到焦点
设置在文本框中
原因对于标签
根据这里的AccessText
definitionfirst'char
的意思
元素和“”的Accesskey
不可见
使用以下链接作为参考:
参见以下代码:
<StackPanel>
<Label Content="_first_second" Target="{Binding ElementName=txtbox}"/>
<TextBox Name="txtbox" Width="100" Height="50"/>
</StackPanel>
现在,在加载了屏幕后
按任意“f”键,您将看到焦点
设置在文本框中
原因对于标签
根据这里的AccessText
definitionfirst'char
的意思
元素和“”的Accesskey
不可见
使用以下链接作为参考:
这是一种精心设计的行为。看:谢谢!这就解决了我的问题。这是一种精心设计的行为。看:谢谢!这就解决了我的问题。谢谢回复!在我的例子中,用TextBlock(如前面提到的swiszcz)替换DataTemplate中的标签要容易得多。@User95 yes。但你的问题只是为什么会这样?这就是为什么我没有为你的问题更换XAML…啊哈,好的。我误解了你的回答。那么你是对的;)谢谢回复!在我的例子中,用TextBlock(如前面提到的swiszcz)替换DataTemplate中的标签要容易得多。@User95 yes。但你的问题只是为什么会这样?这就是为什么我没有为你的问题更换XAML…啊哈,好的。我误解了你的回答。那么你是对的;)