简体   繁体   English

属性绑定中的WPF'_'字符不起作用

[英]WPF '_' character in Property-Binding does not work

On the MainWindow of my WPF application I have a simple listBox that is databound to an ObservableCollection. 在WPF应用程序的MainWindow上,我有一个简单的listBox,它已数据绑定到ObservableCollection。 The ObservableCollection contains members of a simple "Product" class with one string-property. ObservableCollection包含具有一个字符串属性的简单“ Product”类的成员。 The goal is to show the text that is stored in the "PName" property of all Products of the ObservableCollection. 目标是显示存储在ObservableCollection的所有产品的“ PName”属性中的文本。

The MainWindow.xaml: MainWindow.xaml:

<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>

The MainWindow.Xaml.Cs: 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;
        }
    }
}

And now my question: Why is only every second '_' character shown in the listBox output? 现在我的问题是:为什么listBox输出中仅显示第二个'_'字符? The Output-Items should be: "Toaster", "Big_Toaster" and "Very_Big_Toaster" but however I get another output: Output-Items应该是:“ Toaster”,“ Big_Toaster”和“ Very_Big_Toaster”,但是我得到了另一个输出:

Output-Items in listBox: Toaster, BigToaster, VeryBig_Toaster listBox中的输出项:Toaster,BigToaster,VeryBig_Toaster

See below code: 见下面的代码:

<StackPanel>
    <Label Content="_first_second" Target="{Binding ElementName=txtbox}"/>
    <TextBox Name="txtbox" Width="100" Height="50"/>
</StackPanel>

Now after the screen is Loaded press key 'f' any you'll see the Focus is set in the TextBox . 现在,在屏幕Loaded后,按“ f”键,您将看到在TextBox设置了Focus

Cause the the content of Label is treated as AccessText for Label here and according to AccessText definition first ' ' char means the Accesskey to element and ' ' is not visible. 原因标签的内容被视为AccessTextLabel在这里和根据AccessText定义第一“” charAccesskey到元件和“”是不可见的。

use below links for ref: 使用以下链接作为参考:

MSDN on AccessText AccessText上的MSDN

AccessText example AccessText示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM