【问题标题】:WPF - Bind to selecteditem of listbox between user controlsWPF - 绑定到用户控件之间的列表框的选定项
【发布时间】:2011-10-16 00:33:04
【问题描述】:

我有两个用户控件,第一个带有一个绑定到客户列表的列表框,该列表显示每个客户的一些简单详细信息。

第二个用户控件我想更详细地了解在第一个用户控件的列表框中选择的任何客户。

是否可以在第二个控件中设置绑定以绑定到第一个用户控件中的选定项?

我的列表框:

            <ListBox Name="lstCustomer" ItemsSource="{Binding Customers}" >           
                <ListBox.Resources>

                    <DataTemplate DataType="{x:Type MyApplication:Customers}">
                       <Label Grid.Row="0" Content="{Binding Customer.name}" FontSize="14" FontWeight="Bold" Padding="5" />                             
                                <Label Grid.Row="1" Grid.Column="0" Content="{Binding Customer.telephone}" Padding="10,5" />                 
                            </Grid>
                        </Grid>

                    </DataTemplate>
                </ListBox.Resources>
            </ListBox>

详细视图用户控件(到目前为止)

 <Grid x:Name="containingGrid" DataContext="{Binding ElementName=lstCustomers, Path=SelectedItem}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Customer.name}" FontSize="23"/>
        </Grid>

谢谢 格雷格

【问题讨论】:

  • 如果您将 TextBlock 绑定更改为 Text="{Binding name}",您应该可以使用。我建议您在 Customer 的属性声明中将“名称”更改为“名称”,以将其标识为属性并使其遵循一些“规则!”。

标签: wpf data-binding user-controls listbox selecteditem


【解决方案1】:

我建议在您的客户对象的 ViewModel 中有一个属性说 SelectedCustomer 并将其绑定到您的列表框的 SelectedItem,如下所示 -

<ListBox Name="lstCustomer" ItemsSource="{Binding Customers}"
                            SelectedItem = "{Binding SelectedCustomer}" >           
               . . . . .
 </ListBox>

由于您提到两个用户控件都在同一个视图中,所以我假设它们共享同一个 ViewModel。在这种情况下,您可以通过这种方式简单地设置数据上下文 -

<Grid x:Name="containingGrid" DataContext="{Binding SelectedCustomer}">
  <Grid.RowDefinitions>
       <RowDefinition Height="Auto"/>
       <RowDefinition Height="Auto"/>
       <RowDefinition Height="Auto"/>
   </Grid.RowDefinitions>
   <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto"/>
       <ColumnDefinition Width="Auto"/>
   </Grid.ColumnDefinitions>
   <TextBlock Text="{Binding Name}" FontSize="23"/>
</Grid> 

【讨论】:

    【解决方案2】:

    是的,您可以 - 如果您为列表框指定 CustomerList 的名称,那么您可以使用“{Binding ElementName=CustomerList, Path=SelectedItem}”之类的绑定绑定到其 SelectedItem 属性。

    【讨论】:

    • @Mark.. 如果两个列表框都在同一个用户控件中,您的解决方案就可以工作。但根据 greg 他很清楚 bth 列表框处于不同的用户控制中
    • @greg.. 您是否在另一个用户控件或 Window.. 中同时使用这两个用户控件?如果可能,请尝试发布您的 xaml 代码.. 至少简化一个
    • @bathineni 是的,两个用户控件都在同一个用户控件中使用,并且一次只会有一个实例。我在原始问题中发布了详细视图用户控件的 xaml
    • 啊,抱歉,我看错了第一句话。您可以通过 Control1 上的属性公开 ListBox 的 SelectedItem,然后从 Control2 对它进行元素到元素的绑定。这似乎不像它可以做的那样优雅,当然。
    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2011-02-26
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多