Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用数据绑定在WPF列表框中显示单个项目名称?_C#_Wpf_Mvvm_Data Binding_Listbox - Fatal编程技术网

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

C# 如何使用数据绑定在WPF列表框中显示单个项目名称?,c#,wpf,mvvm,data-binding,listbox,C#,Wpf,Mvvm,Data Binding,Listbox,在我的WPF应用程序中,我的主屏幕上有一个列表框。我尝试使用MVVM模式,因此我有一个与视图关联的ViewModel。当我启动应用程序时,我的ViewModel被启动,它读取我放在目录中的一堆DLL。每个DLL都包含一个“Strategy”类,因此当我读取DLL时,我检索这些Strategy类对象并将它们放在一个列表(实际上是一个ObservableCollection)中,该列表是ViewModel的一个成员。我使用这个名为DllList的成员列表来填充列表框 我的ViewModel如下所示(

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

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

公共类ViewModelPDMain:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
私有void RaisePropertyChanged(字符串propertyName){
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共ViewModelPDMain(){
dllList=新的ObservableCollection();
selectedStrategy=新策略();
}
private observeCollection dllList=null;
私有策略selectedStrategy=null;
公共可观测集合数据集
{
获取{return dllList;}
设置{
dllList=值;
RaisePropertyChanged(“DllList”);
}
}
公共战略选择战略
{
获取{return selectedStrategy;}
设置{
选择策略=价值;
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>

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

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

因此,我认为:

<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绑定到文本框文本属性,结果成功了。也就是说,当我单击任何空列表框项目时,它会在文本框中显示StrategyName。请参考下图。您可以看到列表框包含项目,但不显示内容值。此外,在右侧,“策略名称”文本框是一个文本框,我已在其中绑定SelectedStrategy.StratInfo.StrategyName,并在“项目选择”事件中显示正确的值

我在一个更简单的项目中也做过同样的事情,效果很好。我搞不清楚我做错了什么


有什么想法吗?

您在数据模板中的绑定不正确。数据模板中的数据上下文是DllList中的一个项,属于Strategy类型。所以你的标签应该是这样的:

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

数据模板中的绑定不正确。数据模板中的数据上下文是DllList中的一个项,属于Strategy类型。所以你的标签应该是这样的:

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