繁体   English   中英

如何使用数据绑定在WPF列表框中显示单个项目名称?

[英]How to display individual item names in a WPF ListBox using Data Binding?

在WPF应用程序中,主屏幕上有一个ListBox。 我正在尝试使用MVVM模式,所以我有一个与View关联的ViewModel。 当我启动该应用程序时,我的ViewModel会被启动,并读取我放置在目录中的一堆DLL。 每个DLL都包含一个“ Strategy”类,因此,当我阅读这些DLL时,我将检索这些Strategy类对象并将它们放在一个列表(实际上是ObservableCollection)中,该列表是我的ViewModel的成员。 我正在使用这个名为DllList的成员列表来填充ListBox。

我的ViewModel如下所示(为清楚起见,删除了不必要的位):

public class ViewModelPDMain : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public ViewModelPDMain() {
        dllList = new ObservableCollection<Strategy>();
        selectedStrategy = new Strategy();
    }

    private ObservableCollection<Strategy> dllList = null;
    private Strategy selectedStrategy = null;
    public ObservableCollection<Strategy> DllList
    {
        get { return dllList; }
        set {
            dllList = value;
            RaisePropertyChanged("DllList");
        }
    }

    public Strategy SelectedStrategy
    {
        get { return selectedStrategy; }
        set {
            selectedStrategy = value;
            RaisePropertyChanged("SelectedStrategy");
        }
    }
}

然后在我的主视图中,将其绑定如下。

<Window x:Class="PrisonersDilemma.Source.View.ViewPDMain"
        xmlns:local="clr-namespace:PrisonersDilemma.Source.View"
        DataContext="{Binding Source={StaticResource mainViewModelLocator}, Path=ViewModelPDMain}"
        Title="Iterated Prisoner's Dilemma" Height="500" Width="800" MinHeight="500" MinWidth="800">
    <Grid Name="gridMain">
        ...
        <!-- More stuff here -->
        ...
        <ListBox Name="listStrategies" SelectedIndex="0"
                 ItemsSource="{Binding DllList}" SelectedItem="{Binding SelectedStrategy}"
                 Grid.Column="0" Grid.Row="1" Grid.RowSpan="2"
                 Width="Auto" MinWidth="120"
                 Margin="3"
                 BorderBrush="LightGray" BorderThickness="1">

        </ListBox>
        ...
        <!-- More stuff here -->
        ...
    </Grid>
</Window>

当我这样做并运行应用程序时,我的列表框如下所示。

在此处输入图片说明

问题是当我尝试在我的策略对象中显示属性时。 我的策略类包含另一个名为StratInfo的类,该类又包含一个字符串属性StrategyName。 我的要求是将此字符串值显示为列表框项目值,而不是上面显示的内容。

因此,我在视图中执行以下操作:

<Window x:Class="PrisonersDilemma.Source.View.ViewPDMain"
        xmlns:local="clr-namespace:PrisonersDilemma.Source.View"
        DataContext="{Binding Source={StaticResource mainViewModelLocator}, Path=ViewModelPDMain}"
        Title="Iterated Prisoner's Dilemma" Height="500" Width="800" MinHeight="500" MinWidth="800">
    <Grid Name="gridMain">
        ...
        <!-- More Stuff Here -->
        ...
        <ListBox Name="listStrategies" SelectedIndex="0"
                 ItemsSource="{Binding DllList}" SelectedItem="{Binding SelectedStrategy}"
                 Grid.Column="0" Grid.Row="1" Grid.RowSpan="2"
                 Width="Auto" MinWidth="120"
                 Margin="3"
                 BorderBrush="LightGray" BorderThickness="1">
            <!-- Added Stuff -->
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Name="lblFirstName" 
                               Content="{Binding SelectedStrategy.StratInfo.StrategyName, Mode=OneWay}"
                               Grid.Column="0"></Label>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        ...
        <!-- More Stuff Here -->
        ...
    </Grid>
</Window>

当我这样做时,我希望列表框项目包含一个标签,并显示我的StrategyName值。 但是,我得到一个包含25个项目的列表框(我有25个DLL),但是所有25个项目都是空的。

有趣的是,我尝试将SelectedStrategy.StratInfo.StrategyName绑定到文本框Text属性,并且它起作用了。 也就是说,当我单击任何空的列表框项目时,它将在文本框中显示StrategyName。 请参考下图。 您可以看到列表框包含项目,但未显示内容值。 另外,在右边,“策略名称”文本框是一个文本框,在其中我绑定了SelectedStrategy.StratInfo.StrategyName,它在项目选择事件上显示了正确的值。

在此处输入图片说明

我在一个简单的项目中做了同样的事情,而且效果很好。 我无法弄清楚我在做什么错。

有什么想法吗?

您在数据模板中的绑定不正确。 数据模板中的数据上下文是DllList中的一项,其类型为Strategy。 因此,您的标签应如下所示:

<Label Name="lblFirstName"
       Content="{Binding StratInfo.StrategyName, Mode=OneWay}"
       Grid.Column="0"/>

暂无
暂无

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

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