none
ListBox数据绑定的问题 RRS feed

  • 问题

  • 我做了一个ListBoxItem的模版(模版定义在一个单独的资源字典中):

        <Style x:Key="IMChatListItem" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border BorderBrush="Red" BorderThickness="0" Height="56" Name="border" Padding="6 8 6 8">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="56"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Image Clip="{StaticResource clipGeometry40}" Source="{Binding Portrait}" Width="40" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center"></Image>
                                <Border Grid.Column="1">
                                    <Grid>
                                        <TextBlock Text="{Binding UserName}" FontSize="14" Foreground="#333"></TextBlock>
                                        <TextBlock Text="{Binding LastMessageTime,StringFormat='{}{0:HH:mm}',ConverterCulture=zh-CN}" FontSize="12" Foreground="#555" HorizontalAlignment="Right"></TextBlock>
                                        <TextBlock Name="UnreadCount" Text="{Binding UnreadCount}" Width="16" Height="16" Clip="{StaticResource clipGeometry16}" Background="Red" Foreground="#fff" TextAlignment="Center" HorizontalAlignment="Right" VerticalAlignment="Bottom"></TextBlock>
                                        <Button Click="IMChatList_CloseItem" Name="CloseBtnx" Visibility="Hidden" Width="16" Height="16" FontSize="12" Clip="{StaticResource clipGeometry16}" Style="{StaticResource SmallIconButton}" Content="&#xe602;" HorizontalAlignment="Right" VerticalAlignment="Bottom"></Button>
                                    </Grid>
                                </Border>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    注意其中有一个按钮,按钮的click事件我写在一个cs文件里面,现在的问题是,我如何在click事件的代码里面,访问到和这个ListBox绑定的数据?

    我尝试这样写:

            private void IMChatList_CloseItem(object sender, RoutedEventArgs e)
            {
                DependencyObject o = e.Source as DependencyObject;            
                while (o!=null)
                {
                    Console.WriteLine(o.GetType().ToString());
                    if(o is ListBoxItem)
                    {
                    }
                    o = VisualTreeHelper.GetParent(o);
                }
            }        

    我这样取到的只是ListBoxItem对象,并不是我绑定的数据源,也就无法获取这个item的实际数据了。


    da jia hao!

    2017年9月20日 7:32

答案

  • Hi,

    MyObject current = (MyObject)o.DataContext;

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 liubin 2017年9月27日 11:05
    2017年9月27日 7:44
    版主

全部回复

  • Hi,

    >>我这样取到的只是ListBoxItem对象,并不是我绑定的数据源,也就无法获取这个item的实际数据了。

    MyObject current = o as MyObject;

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年9月23日 10:57
    版主
  • 不行的,会提示不能转换对象


    da jia hao!

    2017年9月25日 2:43
  • Hi,

    MyObject current = (MyObject)o.DataContext;

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 liubin 2017年9月27日 11:05
    2017年9月27日 7:44
    版主