【问题标题】:WPF: bind SelectedItem of nested ListBox to property C#WPF:将嵌套列表框的 SelectedItem 绑定到属性 C#
【发布时间】:2017-10-26 13:49:28
【问题描述】:

我有主 DataTemplate ListBox 有 SelectedItem = SelectedSession 我还有一个嵌套的 ListBox,它有 SelectedItem = AssignedExercises 如果我将属性绑定到两个选定的项目,我无法获得嵌套 ListBoxe 的项目 AssignedExercises。

您知道如何访问嵌套的 ListBoxes SelectedItem (AssignedExercises) 并将其绑定到属性吗?

XAML 代码:

<ListBox Grid.Row="1" ItemsSource="{Binding SessionList}" SelectedItem="{Binding SelectedSession}" Margin="0,0,0,20">
<ListBox.ItemTemplate>
    <DataTemplate>
        <Border CornerRadius="2" BorderBrush="Black" BorderThickness="2" Width="1000" Height="200" Margin="1">
            <Grid>

                <Label Content="List of exercises" RenderTransformOrigin="0.5,0.5" Height="30" VerticalAlignment="Top" FontWeight="Bold"></Label>

                <ListBox Height="150" Width="325" ItemsSource="{Binding AssignedExercises}" SelectedItem="{Binding AssignedExercises}">

                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border Height="50" Width="300" BorderBrush="LightGray" BorderThickness="2" CornerRadius="2" Margin="0,1,0,1">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="40"/>
                                    </Grid.RowDefinitions>
                                    <StackPanel Grid.Row="0" Margin="5,0,5,0" VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
                                        <Label Content="{Binding Name}" FontSize="14" FontWeight="Bold"/>
                                    </StackPanel>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </Grid>
        </Border>
    </DataTemplate>
</ListBox.ItemTemplate>

属性:

private TrainingSessionVM selectedSession;
private ExerciseVM selectedExercise;
public TrainingSessionVM SelectedSession
{
    get { return selectedSession; }
    set
    {
        selectedSession = value;
        RaisePropertyChanged();
    }
}
public ExerciseVM SelectedExercise
{
    get { return selectedExercise; }
    set
    {
        selectedExercise = value;
        RaisePropertyChanged();
    }
}

【问题讨论】:

    标签: c# wpf mvvm data-binding


    【解决方案1】:

    如果我正确理解您的问题,您有一个Session 列表,每个列表都包含一个Exercise 列表。这些中的每一个都可以被视为一个视图模型。

    您的主视图模型有一个SelectedSession 来指示选择了哪个Session。您只需要在Session 中做同样的事情(不是您的示例中的主视图模型)。将SelectedExercise添加到Session,并将内部ListBoxSelectedItem绑定到它。

    然后,您可以使用 SelectedSession.SelectedExercise 从主视图模型访问内部选定的 Exercise

    【讨论】:

    • 谢谢你的答案。两个绑定都在 TrainingSessionMainVM 中。如果我添加 SelectedItem="{Binding SelectedSession.AssignedExercises}" 没有任何反应。属性没有任何价值。
    • 对,我是说你应该改变它。将 SelectedExercise 属性移动到 TrainingSessionVM。您将可以轻松地以这种方式绑定到它,并且您仍然可以从外部视图模型访问它。
    • 您在内部 ListBox 上的绑定看起来像 {Binding Path=SelectedExercise},该属性将位于不同的对象上。
    • 这个属性已经存在并且从一开始就存在。如果它与 SelectedSession.SelectedExercise 或 SelectedExercise 绑定,我也无法访问 SelectedExercise 的选定项目。
    • 您的示例代码显示SelectedExercise 属性是您的主视图模型的成员,而不是TrainingSessionVM
    【解决方案2】:

    在这里我找到了如何通过DataContext绑定直接绑定的解决方案:

    SelectedItem="{Binding DataContext.SelectedExercise, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
    

    但在这种情况下,应该以这种方式绑定到视图模型:

    <Grid.DataContext>
        <Binding Source="{StaticResource Locator}" Mode="OneWay" Path="TrainingSessionMain"/>
    </Grid.DataContext>
    

    感谢参与。

    【讨论】:

    • 嘿!很抱歉看到你删除了你的问题,我只是想提供有用的反馈来改进它。在您确认阅读后,我将删除我的评论,这是关于您的反思问题(您没有 20 个代表,所以我无法与您聊天)。查看dotnetfiddle.net/WpbNbU,如果这是您想要的,请告诉我!
    • 你确认后我会删除这2个cmets :)
    • @AlexC。谢谢你的尝试。您的代码与我的代码相同。因为我没有透露整个代码。但我需要操纵运行 WinForm 应用程序的不同方法。哦 = Activator.CreateInstanceFrom(executable, type); Application.Run((Form)oh.Unwrap());我这样做是在这样的载人模式下,你可以看到启动的应用程序的实例被分离到单独的线程中,更多的是它只适用于未包装的形式。但我需要在运行时完全控制任何方法的运行应用程序。
    猜你喜欢
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2010-11-01
    • 2021-04-11
    • 1970-01-01
    • 2013-02-22
    • 2016-05-23
    相关资源
    最近更新 更多