【问题标题】:How can i set binding to childproperty from App.xaml如何从 App.xaml 设置绑定到子属性
【发布时间】:2012-06-17 00:52:21
【问题描述】:

我在绑定到 ListBox 控件时遇到问题。 实际上我在 App.xaml.cs 中有一个属性:

public partial class App : Application, INotifyPropertyChanged
{
    ObservableCollection<Panier> _panier = new ObservableCollection<Panier>();

    public ObservableCollection<Panier> PanierProperty
    {
        get { return _panier; }
        set
        {
            if (this._panier != value)
            {
                this._panier = value;
                NotifyPropertyChanged("PanierProperty");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

此属性在此处的“Panier 类”中有一个子属性:

public class Panier : INotifyPropertyChanged
{
    private string _nom;
    private string _category;
    private int _prix;

    public string Nom
    {
        get { return _nom; }
        set
        {
            if (this._nom != value)
            {
                this._nom = value;
                NotifyPropertyChanged("Nom");
            }
        }
    }

    public string Category
    {
        get { return _category; }
        set
        {
            if (this._category != value)
            {
                this._category = value;
                NotifyPropertyChanged("Category");
            }
        }
    }

    public int Prix
    {
        get { return _prix; }
        set
        {
            if (this._prix != value)
            {
                this._prix = value;
                NotifyPropertyChanged("Prix");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

在我的 MainWindow.xaml 页面中,我将 ListBox 绑定到 PanierProperty(parent property):

<telerik:RadListBox x:Name="PanierLB" Grid.Row="1" Height="200" Width="350" Margin="0 300 0 0"
    ItemsSource="{Binding PanierProperty, Source={x:Static Application.Current}}"
    DisplayMemberPath="{Binding Path=PanierProperty.Nom, Source={x:Static Application.Current}}">
</telerik:RadListBox>

我的问题是 PanierProperty 绑定到我的列表框我在列表框中看到了像 Design.Panier 这样的项目 设计.帕尼尔 设计.帕尼尔 ETC... 我不知道如何让 PanierProperty.Nom(Nom 是子属性)显示在 ListBox 上。

有人可以帮忙。

【问题讨论】:

    标签: c# wpf binding telerik inotifypropertychanged


    【解决方案1】:

    DisplayMemberPath 中使用您只想显示的属性名称:

    <telerik:RadListBox
        ...
        DisplayMemberPath="Nom"
    

    【讨论】:

    • 非常感谢它让我发疯:s 我只是尝试使用 {Binding Nom} 不知道谢谢:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多