ColorPicker Custom Style Designer An Exception was thrown.

2 Answers 141 Views
ColorPicker Styling
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Psyduck asked on 27 Apr 2021, 12:27 PM | edited on 29 Apr 2021, 01:46 AM

Hellp

 

In the previous post I asked a question and got an answer..
But there was a problem.

It is related to number 1. ( https://www.telerik.com/forums/is-a-custom-color-picker-possible )

 

It runs fine until the build is successful and I start debugging and finish.

Using <Setter Property = "local : CustomColorPickerOpenBehavior"Value = "True"/> crashes the designer.

Where xaml designer Exception was thrown.

 

InvalidOperationException: Sequence contains no matching element

 

 

Deleting the CustomColorPickerOpenBehavior property shows the xaml designer.

If the property is created again, an exception occurs.

 

What is the problem?

Thanks.


<Style TargetType="telerik:RadColorPicker" BasedOn="{StaticResource RadColorPickerStyle}">
	<Setter Property="Padding" Value="5" />
	<Setter Property="SplitButtonStyle">
		<Setter.Value>
			<Style TargetType="telerik:RadSplitButton" BasedOn="{StaticResource RadSplitButtonStyle}">
				<Setter Property="mat:MaterialAssist.FocusBrush"		Value="Transparent"/>
				<Setter Property="mat:MaterialAssist.MouseOverBrush"	Value="Transparent"/>
				<Setter Property="behaviour:ColorPickerButtonSync.IsEnabled" Value="True" />
				<Setter Property="IsChecked" Value="{Binding IsDropDownOpen, RelativeSource={RelativeSource AncestorType=telerik:RadColorPicker}}" />
			</Style>
		</Setter.Value>
	</Setter>
</Style>

<telerik:RadColorPicker Width="70" SelectedColor="Red" IsRecentColorsActive="True">
	<telerik:RadColorPicker.ContentTemplate>
		<DataTemplate>
			<Rectangle	Width="35" Height="25"
									Fill="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadColorPicker}},
									Path=SelectedColor, Converter={StaticResource mediaToBrushConverter}}" />
		</DataTemplate>
	</telerik:RadColorPicker.ContentTemplate>

	<telerik:RadColorPicker.AdditionalContent>
		<ContentControl HorizontalContentAlignment="Stretch" Content="{Binding}" >
			<ContentControl.ContentTemplate>
				<DataTemplate>
					<Grid>
						<telerik:RadButton	Height="26" BorderThickness="0"
														HorizontalContentAlignment="Left"
														Command="{Binding OnClickOpenEditColorsCommand}"
														CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type telerik:RadColorPicker}}}"
														>
							<telerik:RadButton.Content>
								<TextBlock Text="More Colors..." Margin="26 0 0 0" />
							</telerik:RadButton.Content>
						</telerik:RadButton>
					</Grid>
				</DataTemplate>
			</ContentControl.ContentTemplate>
		</ContentControl>
	</telerik:RadColorPicker.AdditionalContent>
</telerik:RadColorPicker>

 

public class ColorPickerButtonSync
    {
        public static bool GetIsEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsEnabledProperty);
        }

        public static void SetIsEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsEnabledProperty, value);
        }

        public static readonly DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ColorPickerButtonSync), new PropertyMetadata(false, new PropertyChangedCallback(OnIsEnabledChanged)));

        private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var button = d as RadSplitButton;
            button.IsToggle = true;
            button.Loaded += Button_Loaded;
            button.Checked += SplitButton_Checked;
            button.Unchecked += SplitButton_Unchecked;
            button.DropDownOpened += Button_DropDownOpened;
            button.DropDownClosed += Button_DropDownClosed;
        }

        private static void Button_Loaded(object sender, RoutedEventArgs e)
        {
            var splitButton = sender as RadSplitButton;
            var colorSelector = splitButton.DropDownContent as RadColorSelector;
            colorSelector.Loaded += (s, a) =>
            {
                var rootElement = colorSelector.ChildrenOfType<Grid>().First(x => x.Name == "RootElement");
                Grid.SetRow(rootElement.Children[1], 4);
                Grid.SetRow(rootElement.Children[2], 5);
                Grid.SetRow(rootElement.Children[3], 6);
                Grid.SetRow(rootElement.Children[4], 1);
                Grid.SetRow(rootElement.Children[5], 2);
                Grid.SetRow(rootElement.Children[6], 3);
            };

            var dropDownPart = splitButton.ChildrenOfType<RadToggleButton>().First(x => x.Name == "DropDownPart");
            var rectangle = splitButton.ChildrenOfType<Rectangle>().First(x => x.Name == "Separator");
            //System.InvalidOperationException: 'Sequence contains no matching element'
            dropDownPart.Visibility = Visibility.Collapsed;
            rectangle.Visibility = Visibility.Collapsed;
        }

        private static void Button_DropDownClosed(object sender, RoutedEventArgs e)
        {
            var splitButton = sender as RadSplitButton;
            splitButton.IsChecked = false;
        }

        private static void Button_DropDownOpened(object sender, RoutedEventArgs e)
        {
            var splitButton = sender as RadSplitButton;
            splitButton.IsChecked = true;
        }

        private static void SplitButton_Checked(object sender, RoutedEventArgs e)
        {
            var splitButton = sender as RadSplitButton;
            var colorPicker = splitButton.ParentOfType<RadColorPicker>();
            colorPicker.IsDropDownOpen = true;
        }

        private static void SplitButton_Unchecked(object sender, RoutedEventArgs e)
        {
            var splitButton = sender as RadSplitButton;
            var colorPicker = splitButton.ParentOfType<RadColorPicker>();
            colorPicker.IsDropDownOpen = false;
        }
    }



 

 

2 Answers, 1 is accepted

Sort by
1
Accepted
Dilyan Traykov
Telerik team
answered on 29 Apr 2021, 01:56 PM

Hello,

Thank you for the provided images and code snippets.

I tested this with the sample project I last provided in the forum thread you referenced with the latest version of Visual Studio 2019, however, I do not observe any exceptions, and the RadColorPicker control is displayed as expected at my end. Here's a screenshot of the result I observe after building the project:

What I can suggest is to either try this with the latest version of Visual Studio or add the following if statement to avoid the exception:

        private static void Button_Loaded(object sender, RoutedEventArgs e)
        {
            var splitButton = sender as RadSplitButton;
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(splitButton))
            {
                var dropDownPart = splitButton.ChildrenOfType<RadToggleButton>().First(x => x.Name == "DropDownPart");
                var rectangle = splitButton.ChildrenOfType<Rectangle>().First(x => x.Name == "Separator");
                dropDownPart.Visibility = Visibility.Collapsed;
                rectangle.Visibility = Visibility.Collapsed;
                var colorSelector = splitButton.DropDownContent as RadColorSelector;
                colorSelector.Loaded += (s, a) =>
                {
                    var rootElement = colorSelector.ChildrenOfType<Grid>().First(x => x.Name == "RootElement");
                    Grid.SetRow(rootElement.Children[1], 4);
                    Grid.SetRow(rootElement.Children[2], 5);
                    Grid.SetRow(rootElement.Children[3], 6);
                    Grid.SetRow(rootElement.Children[4], 1);
                    Grid.SetRow(rootElement.Children[5], 2);
                    Grid.SetRow(rootElement.Children[6], 3);
                };
            }
        }

Please let me know if any of these approaches works for you.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
answered on 30 Apr 2021, 01:22 AM

Hello.

 

I am using Visual Studio Community version 16.9.4. This is a post uploaded to the latest version.

Learn the GetIsInDesignMode function. This avoids the designer exception. I will use this.

 

DesignerProperties.GetIsInDesignMode doesn't catch breakpoints even when debugging, so it doesn't affect performance, right?

Is it just the code getting a little dirty?

 

Thanks.

 

Martin Ivanov
Telerik team
commented on 04 May 2021, 09:56 AM

The line DesignerProperties.GetIsInDesignMode will be also evaluated by the debugger. In other words, it catches breakpoints. However, the effect on the performance won't be noticeable.
Tags
ColorPicker Styling
Asked by
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Answers by
Dilyan Traykov
Telerik team
Psyduck
Top achievements
Rank 5
Bronze
Bronze
Bronze
Share this question
or