WPF,如何获取ListBox数据模板中的子元素呢?

时间:2022-04-22 15:48:07
我按照MSDN的 如何:查找由 DataTemplate 生成的元素上面讲的方法,获取ListBox数据模板中的子元素
但是,按照MSDN上面的解释,必须首先获取到 ListBoxItem ,然后再在 ListBoxItem内查找 ContentPresenter,然后对在该 ContentPresenter 上设置的 DataTemplate 调用 FindName方法。要绕这么大一个弯才能找到数据模板中的子元素。
但是,如果ListBox并没有添加任何项,是不是就无法获取到DataTemplate中的子元素了呢?因为此时并没有ListBoxItem 。
你知道吗?

7 个解决方案

#2


LZ的WPF用成了低级的Winform了,建议学习学习Prism
就lz的问题而言,自己调试,完全能出来
后台添加断点,用一个临时变量得到ListBox所有的Item,调试时,添加快速监视,然后一点点找  就ok了

#3


引用 2 楼 yanbuodiao 的回复:
LZ的WPF用成了低级的Winform了,建议学习学习Prism
就lz的问题而言,自己调试,完全能出来
后台添加断点,用一个临时变量得到ListBox所有的Item,调试时,添加快速监视,然后一点点找  就ok了

有Item当然就好办些了,请看清楚我的问题。
假如一个自定义的ListBox,想要在OnApplyTemplate方法中获取数据模板中的子元素,能实现吗?

#4


<ListBox Grid.Row="1" ItemsSource="{Binding WasteReasonList}" SelectedValue="{Binding SelectWasteReason}"  SelectionMode="Single"  ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top">
                        <ListBox.Resources>
                            <Style TargetType="ListBoxItem">
                                <Style.Resources>
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                                </Style.Resources>
                            </Style>
                        </ListBox.Resources>
                        <ListBox.Style>
                            <Style TargetType="ListBox" BasedOn="{StaticResource MedListBoxStyle}">
                                <Setter Property="BorderThickness" Value="0"></Setter>
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="Margin" Value="0,0,0,0"></Setter>
                                <Setter Property="FontStyle" Value="Normal"></Setter>
                                <Setter Property="MaxHeight" Value="180"></Setter>
                                <Style.Triggers>
                                    <Trigger Property="IsEnabled" Value="False">
                                        <Setter Property="Background" Value="Transparent"></Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </ListBox.Style>
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel Orientation="Vertical" />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <WrapPanel  Orientation="Vertical" Margin="0,0,0,0">
                                    <RadioButton Margin="25,8,0,0" Content="{Binding DataDictionaryInfo.DisplayName,Mode=OneWay}" FontSize="7" FontWeight="Normal" FontStyle="Normal" GroupName="a"
                                                 IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},Path=IsSelected}" 
                                                 Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.CheckCommand}" 
                                                 CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},Path=DataContext}">
                                        <RadioButton.LayoutTransform>
                                            <ScaleTransform ScaleX="2" ScaleY="2" />
                                        </RadioButton.LayoutTransform>
                                    </RadioButton>
                                </WrapPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>




//其他代码
     private IList<WasteReasonInfo> wasteReasonList;
        public IList<WasteReasonInfo> WasteReasonList
        {
            get { return wasteReasonList; }
            set
            {
                if (wasteReasonList != value)
                {
                    wasteReasonList = value;
                    RaisePropertyChanged("WasteReasonList");
                    if (wasteReasonList != null && wasteReasonList.Count > 0 && SelectWasteReason == null)
                    {
                        SelectWasteReason = WasteReasonList[0];
                    }
                }
            }
        }

        private WasteReasonInfo selectWasteReason;
        public WasteReasonInfo SelectWasteReason
        {
            get { return selectWasteReason; }
            set
            {
                if (selectWasteReason != value)
                {
                    selectWasteReason = value;
                    RaisePropertyChanged("SelectWasteReason");
                    if (selectWasteReason != null)
                        selectWasteReason.IsChecked = true;
                    isInputReason = false;
                    otherReasonString = null;
                    RaisePropertyChanged("IsInputReason");
                    RaisePropertyChanged("OtherReasonString");
                }
            }
        }
//其他代码


上面是我们现在用法,后台仅操作数据就行
另外我没有找到你所说的OnApplyTemplate,是ListBox的事件触发的方法?哪个事件?

#5


OnApplyTemplate是自定义控件类重写的啊

#6


如果没有创建ListBoxItem,能获取到数据模板中的子元素吗?

#7


是不是只有在ListBox有项的情况下,才能获取到数据模板中的子元素呢?

#1


#2


LZ的WPF用成了低级的Winform了,建议学习学习Prism
就lz的问题而言,自己调试,完全能出来
后台添加断点,用一个临时变量得到ListBox所有的Item,调试时,添加快速监视,然后一点点找  就ok了

#3


引用 2 楼 yanbuodiao 的回复:
LZ的WPF用成了低级的Winform了,建议学习学习Prism
就lz的问题而言,自己调试,完全能出来
后台添加断点,用一个临时变量得到ListBox所有的Item,调试时,添加快速监视,然后一点点找  就ok了

有Item当然就好办些了,请看清楚我的问题。
假如一个自定义的ListBox,想要在OnApplyTemplate方法中获取数据模板中的子元素,能实现吗?

#4


<ListBox Grid.Row="1" ItemsSource="{Binding WasteReasonList}" SelectedValue="{Binding SelectWasteReason}"  SelectionMode="Single"  ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" VerticalAlignment="Top">
                        <ListBox.Resources>
                            <Style TargetType="ListBoxItem">
                                <Style.Resources>
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                                </Style.Resources>
                            </Style>
                        </ListBox.Resources>
                        <ListBox.Style>
                            <Style TargetType="ListBox" BasedOn="{StaticResource MedListBoxStyle}">
                                <Setter Property="BorderThickness" Value="0"></Setter>
                                <Setter Property="Background" Value="Transparent" />
                                <Setter Property="Margin" Value="0,0,0,0"></Setter>
                                <Setter Property="FontStyle" Value="Normal"></Setter>
                                <Setter Property="MaxHeight" Value="180"></Setter>
                                <Style.Triggers>
                                    <Trigger Property="IsEnabled" Value="False">
                                        <Setter Property="Background" Value="Transparent"></Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </ListBox.Style>
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel Orientation="Vertical" />
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <WrapPanel  Orientation="Vertical" Margin="0,0,0,0">
                                    <RadioButton Margin="25,8,0,0" Content="{Binding DataDictionaryInfo.DisplayName,Mode=OneWay}" FontSize="7" FontWeight="Normal" FontStyle="Normal" GroupName="a"
                                                 IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},Path=IsSelected}" 
                                                 Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.CheckCommand}" 
                                                 CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem},Path=DataContext}">
                                        <RadioButton.LayoutTransform>
                                            <ScaleTransform ScaleX="2" ScaleY="2" />
                                        </RadioButton.LayoutTransform>
                                    </RadioButton>
                                </WrapPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>




//其他代码
     private IList<WasteReasonInfo> wasteReasonList;
        public IList<WasteReasonInfo> WasteReasonList
        {
            get { return wasteReasonList; }
            set
            {
                if (wasteReasonList != value)
                {
                    wasteReasonList = value;
                    RaisePropertyChanged("WasteReasonList");
                    if (wasteReasonList != null && wasteReasonList.Count > 0 && SelectWasteReason == null)
                    {
                        SelectWasteReason = WasteReasonList[0];
                    }
                }
            }
        }

        private WasteReasonInfo selectWasteReason;
        public WasteReasonInfo SelectWasteReason
        {
            get { return selectWasteReason; }
            set
            {
                if (selectWasteReason != value)
                {
                    selectWasteReason = value;
                    RaisePropertyChanged("SelectWasteReason");
                    if (selectWasteReason != null)
                        selectWasteReason.IsChecked = true;
                    isInputReason = false;
                    otherReasonString = null;
                    RaisePropertyChanged("IsInputReason");
                    RaisePropertyChanged("OtherReasonString");
                }
            }
        }
//其他代码


上面是我们现在用法,后台仅操作数据就行
另外我没有找到你所说的OnApplyTemplate,是ListBox的事件触发的方法?哪个事件?

#5


OnApplyTemplate是自定义控件类重写的啊

#6


如果没有创建ListBoxItem,能获取到数据模板中的子元素吗?

#7


是不是只有在ListBox有项的情况下,才能获取到数据模板中的子元素呢?