I am trying to make all my combo boxes re size themselves to the width of their longest item as soon as my view is loaded. I have stumbled upon the following question saying that this is impossible in XAML : How can I make a WPF combo box have the width of its widest element in XAML?.
So, now what I want to do is apply the code found in that initial question, and loop it for all the combo boxes that are in my view. Is there a way, from the code behind of my view, to get all the combo boxes contained in it so I can loop through them and adjust their width, instead of manually having to do it for each combo boxes?
EDIT:
The proposed duplicate answer does not seem to work with UserControl
type views. Here is how my view code behind is declared:
public partial class QuickLookRequestView : UserControl, IView<QuickLookRequestViewModel>
{
private QuickLookRequestViewModel _viewModel;
public QuickLookRequestView()
{
InitializeComponent();
// This cannot be done in XAML
ResizeComboBoxToMaxItemWidth();
}
private void ResizeComboBoxToMaxItemWidth()
{
foreach (ComboBox comboBox in FindVisualChildren<ComboBox>(this))
{
double width = 0;
foreach (ComboBoxItem item in comboBox.Items)
{
item.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
if (item.DesiredSize.Width > width)
{
width = item.DesiredSize.Width;
}
}
comboBox.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
comboBox.Width = comboBox.DesiredSize.Width + width;
}
}
private IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
public QuickLookRequestViewModel ViewModel
{
get { return _viewModel; }
set { _viewModel = value; }
}
}
And while debugging, the application never goes into the foreach (ComboBox comboBox in FindVisualChildren<ComboBox>(this))
part of the code, so it does not find my combo boxes.
EDIT2:
It seems to be the following function in FindVisualChildren
that doesn't find any visual child of my UserControl: for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
EDIT3:
I no longer have the problem from my 1st and 2nd EDIT, though I do have a new one. One of my combo boxes displays items from an enum. This is done this way:
<ComboBox Name="ItemTypeComboBox" Margin="10,5,5,5" ItemsSource="{Binding ComboBoxItemTypes}" SelectedItem="{Binding SelectedComboBoxItemType}"/>
Where ComboBoxItemTypes
is a get
only property defined as follow:
public IEnumerable<EveItem.ItemTypes> ComboBoxItemTypes
{
get
{
return Enum.GetValues(typeof(EveItem.ItemTypes)).Cast<EveItem.ItemTypes>();
}
}
And SelectedComboBoxItemType
is of type ItemTypes
, which is defined this way:
public enum ItemTypes
{
Ore,
Ice,
Gas,
Mineral,
Pi
}
The problem I have is I get the following error message when FindVisualChildren()
loops though that combo box:
Unable to cast object of type 'ItemTypes' to type 'System.Windows.Controls.ComboBoxItem'
How would I modify the FindVisualChildren()
function to work with my combo box and enum type?