gpt4 book ai didi

c# - 如何找到 RibbonComboBox 的子元素

转载 作者:行者123 更新时间:2023-11-30 23:22:53 35 4
gpt4 key购买 nike

我已经在我的 XAML 中声明了一个 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 的一些成员,我需要 RibbonGallery 的其他成员和一些任务需要 RibbonGalleryCategory 的成员。

那么,根据我的 RibbonComboBox 实例,我如何找到子 RibbonGalleryRibbonGalleryCategory 元素?没有 ControlsChildren 属性。

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

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;
}

但是,在编译此代码时,VisualTreeHelper.GetChildrenCount() 在我传递组合框时始终返回 0。因此无法找到控件的子控件。

编辑:

根据名称 VisualTreeHelper,我假设此类是用于查找视觉元素的,并且图库可能不是一个单独的视觉元素。所以我想我需要知道如何遍历不可视的子元素?

最佳答案

我采用了两种不同的方法。一个包含您的嵌套元素,另一个包含一个简单的 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 通过绑定(bind)和 MVVM 释放它真正的力量。我也可以给你举个这样的例子。

编辑

是的,你是对的。 ComboBox-Items 将显示但无法选择,除非您使用 RibbonGalleryCategory-Approach。

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

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

有了 DataBinding,一切都像魅力一样运作。不幸的是,您仍然需要使用 RibbonGalleryRibbonGalleryCategory。或者你只是在那里放置一个 Ribbon 风格的普通 ComboBox

EDIT 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>

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

关于c# - 如何找到 RibbonComboBox 的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626910/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com