如何找到RibbonComboBox的子元素

时间:2016-07-28 04:04:00

标签: c# wpf xaml

我在XAML中声明了element

RibbonComboBox

由于我不完全理解的原因,为了操纵组合框中的项目,我需要<RibbonGroup Header="Category"> <RibbonComboBox Name="cboCategory" Label="Category" HorizontalContentAlignment="Left"> <RibbonGallery Name="galCategory"> <RibbonGalleryCategory Name="catCategory" DisplayMemberPath="Text"> </RibbonGalleryCategory> </RibbonGallery> </RibbonComboBox> <RibbonComboBox Name="cboSubcategory" Label="Subcategory:" HorizontalContentAlignment="Left"> <RibbonGallery Name="galSubcategory"> <RibbonGalleryCategory Name="catSubcategory" DisplayMemberPath="Text"> </RibbonGalleryCategory> </RibbonGallery> </RibbonComboBox> </RibbonGroup> 的一些成员,我需要RibbonComboBox的其他成员,而某些任务需要成员{ {1}}。

因此,根据RibbonGallery的实例,如何找到子RibbonGalleryCategoryRibbonComboBox元素?没有RibbonGalleryRibbonGalleryCategory属性。

我在网上找到了以下代码,用于查找控件的子元素:

Controls

但是,当这段代码编译时,Children总是在我通过我的组合框时返回0。所以无法找到对照的孩子。

修改

通过名称protected T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject { if (depObj == null) return null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); var result = (child as T) ?? GetChildOfType<T>(child); if (result != null) return result; } return null; } ,我假设这个类是用于查找可视元素,并且库可能不是一个单独的可视元素。所以我想我需要知道如何遍历不可视的子元素?

1 个答案:

答案 0 :(得分:2)

我做了两种不同的方法。一个是嵌套元素,一个是简单的ComboBox。按钮填充并阅读ComboBoxRibbonGalleryCategory

<强> XAML

  <StackPanel>
            <Button Content="Fill me :)" Width="80" Height="20" Click="FillMe_OnClick"/>
            <Ribbon>
                <RibbonGroup Header="Category">
                    <RibbonComboBox Name="cboCategory" Label="Category" HorizontalContentAlignment="Left">
                        <ComboBoxItem>Item 1</ComboBoxItem>
                        <ComboBoxItem>Item 2</ComboBoxItem>
                        <ComboBoxItem>Item 3</ComboBoxItem>
                        <ComboBoxItem>Item 4</ComboBoxItem>
                    </RibbonComboBox>
                    <RibbonComboBox Name="cboSubcategory" Label="Subcategory:" HorizontalContentAlignment="Left">
                        <RibbonGallery Name="galSubcategory">
                            <RibbonGalleryCategory Name="catSubcategory" DisplayMemberPath="Text">
                            </RibbonGalleryCategory>
                        </RibbonGallery>
                    </RibbonComboBox>
                </RibbonGroup>
            </Ribbon>
            <Button Content="Read me" Width="80" Height="20" Click="ReadMeCat_OnClick"></Button>
            <Button Content="Read me too" Width="80" Height="20" Click="ReadMeCombo_OnClick"></Button>
</StackPanel>

<强>代码隐藏

public MainWindow()
        {

            InitializeComponent();
        }

        private void FillMe_OnClick(object sender, RoutedEventArgs e)
        {
            this.catSubcategory.Items.Add(new { Text = "Hello" });
            this.catSubcategory.Items.Add(new { Text = "World" });
            this.catSubcategory.Items.Add(new { Text = "Hello" });
            this.catSubcategory.Items.Add(new { Text = "Moon" });
        }

        private void ReadMeCat_OnClick(object sender, RoutedEventArgs e)
        {
            var result = catSubcategory.Items.Cast<dynamic>().Aggregate("", (current, xx) => (string) (current + (xx.Text + "\n")));
            MessageBox.Show(result);
        }

        private void ReadMeCombo_OnClick(object sender, RoutedEventArgs e)
        {
            var result = cboCategory.Items.Cast<ComboBoxItem>().Aggregate("", (current, xx) => current + (xx.Content.ToString() + "\n"));
            MessageBox.Show(result);
        }

注意

我认为这不需要太多解释。如果您仍然需要,请致电;)

请注意,这不是应该如何完成的。 WPF通过Bindings和MVVM释放它的真正力量。我也可以为你提供这样的例子。

修改

是的,你是对的。除非您使用RibbonGalleryCategory - 方法,否则将显示ComboBox项目但无法选择。

关于这个问题的另一个愚蠢的事情是,RibbonGallery实际上并不是真正的控件。它只是一个带有ControlTemplate的ItemsControl,无法找到VisualTreeHelper

在我看来,如果你从一开始就不使用MVVM和DataBinding,那么使用那些控件会使一切变得更加复杂。

使用DataBinding,一切都像魅力一样。很遗憾,您仍然需要使用RibbonGalleryRibbonGalleryCategory。或者你只是放置一个Ribbon风格的常见ComboBox

编辑2 - 简单的MVVM方法

<强>代码

public partial class MainWindow : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public MainWindow()
    {

        InitializeComponent();
        this.Ponies.Add(new Pony() { Id = 0, Color = Brushes.DeepSkyBlue, Name = "Slayer" });
        this.Ponies.Add(new Pony() { Id = 1, Color = Brushes.DeepPink, Name = "Murder" });
        this.Ponies.Add(new Pony() { Id = 2, Color = Brushes.Yellow, Name = "Brutal" });
        this.DataContext = this;
    }

    private ObservableCollection<Pony> _ponies = new ObservableCollection<Pony>();
    private Pony _selectedPony;
    public ObservableCollection<Pony> Ponies => this._ponies;


    public Pony SelectedPony {
        get { return _selectedPony; }
        set {
            if (this._selectedPony == value) return;
            _selectedPony = value;
            this.OnPropertyChanged("SelectedPony");
        }
    }
}

public class Pony : INotifyPropertyChanged
{
    public int Id { get; set; }
    private string _name;

    public string Name {
        get { return this._name; }
        set {
            this._name = value;
            this.OnPropertyChanged("Name");
        }
    }

    public Brush Color { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

<强> XAML

<Window x:Class="MyNameSpace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Name="root"
        Title="Try WPF!"
        mc:Ignorable="d">

    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Ribbon>
            <RibbonGroup Header="Category">
                <RibbonComboBox Label="Category" HorizontalContentAlignment="Left" >
                    <RibbonGallery SelectedItem="{Binding SelectedPony}">
                        <RibbonGalleryCategory ItemsSource="{Binding Ponies}" >
                            <RibbonGalleryCategory.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Name="tb" Text="{Binding Name}" Background="{Binding Color}"/>
                            </DataTemplate>
                            </RibbonGalleryCategory.ItemTemplate>
                        </RibbonGalleryCategory>
                    </RibbonGallery>
                </RibbonComboBox>                
            </RibbonGroup>
        </Ribbon>

    </Grid>
</Window>

如您所见,功能更多,代码更少:)