Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
390
How to make XamComboEditor popup width dependent?
posted

Normally, a XamComboEditor popup's width is as same as the XamComboEditor's. If I change the style of  XamComboEditor and set the MinWidth of the popup as following:

<Popup x:Name="Popup" Grid.ColumnSpan="2" IsOpen="False" VerticalAlignment="Bottom" >
                                    <Border x:Name="RootPopupElement"
                                                       MinWidth="500" ....

, the popup will be wider than the XamComboEditor. But when a item in popup has a very long content, the popup still cannot show the whole content, and a horizontal scroll bar will show. I want that the popup always shows the whole content of each item and the horizontal scroll bar never shows (the popup width depends on the combo item content.) How can I make this happen?

Parents
No Data
Reply
  • 390
    Verified Answer
    posted

    I finally found a solution by myself :-)

    Firstly, bind the MinWidth of the popup with the Tag property of XamComboEditor by setting:

    <Popup x:Name="Popup" Grid.ColumnSpan="2" IsOpen="False" VerticalAlignment="Bottom" >
                                        <Border x:Name="RootPopupElement"
                                                MinWidth="{Binding RelativeSource={RelativeSource TemplatedParent},Mode=OneWay,Path=Tag}" ....

    Secondly, each time after xamComboEditor1.ItemsSource is set, investigate every item in the ItemsSource, find out how wide a textblock should be to show the whole content of the item and set the maximum of the item widthes to the Tag property of xamComboEditor1, as following:

            private void SetPopupMinWidth()
            {
                xamComboEditor1.Tag = null;
                List<string> items = xamComboEditor1.ItemsSource as List<string>;

                if (items == null)
                {
                    return;
                }

                Thickness? padding = null;
                double? fontSize = null;
                double minWidth = 0;
                FontFamily fontFamily = null;

                //find the Padding/FontSize/FontSize value in ItemContainerStyle
                foreach (Setter setter in xamComboEditor1.ItemContainerStyle.Setters)
                {
                    if (setter.Property == ComboEditorItemControl.PaddingProperty)
                    {
                        padding = (Thickness)(setter.Value);
                    }

                    if (setter.Property == ComboEditorItemControl.FontSizeProperty)
                    {
                        fontSize = (double)(setter.Value);
                    }

                    if (setter.Property == ComboEditorItemControl.FontSizeProperty)
                    {
                        fontFamily = setter.Value as FontFamily;
                    }
                }

                foreach (string item in items)
                {
                    TextBlock tb = new TextBlock();
                    tb.Text = item;

                    if (padding != null)
                    {
                        tb.Padding = padding.Value;
                    }

                    if (fontSize != null)
                    {
                        tb.FontSize = fontSize.Value;
                    }

                    if (fontFamily != null)
                    {
                        tb.FontFamily = fontFamily;
                    }

                    if (tb.ActualWidth + 20 > minWidth)
                    {
                        //tb.ActualWidth is the minimal width to show the whole item content.
                        //20 is added for the width of vertical scroll bar of the popup.
                        minWidth = tb.ActualWidth + 20;
                    }
                }

                xamComboEditor1.Tag = minWidth;
            }

    By doing the above, the popup width is made dependent.

     

Children
No Data