Custom Command State

3 posts, 1 answers
  1. Jose Ramon
    Jose Ramon avatar
    2 posts
    Member since:
    Sep 2013

    Posted 23 Sep 2013 Link to this post

    Hi,

    I have a problem whith the CustomCommand of the RadMap. 

    I use a CustomCommand for Add or remove a WMS provider. 

    My problem is the CustomCommand State, when i click the button of the custom command, it is highlighted (this is ok), but when i clicking the button for second time to disable, the button remains highlighted.

    Another problem, when i click the button of the custom command, it is highlighted (this is ok), but when i clik another button (ex. road view), the button not remains highlighted.

    ¿How I can control this?

    Thanks and sorry for my english.
  2. Answer
    Andrey
    Admin
    Andrey avatar
    1681 posts

    Posted 25 Sep 2013 Link to this post

    Hi Jose,

    It is hard to say about particular causes of these problems without your code or your solution.

    1. A reason of this behavior could be in the case when you use for example the RadRadioButton in DataTemplate for your custom command. I think it is correct to use the RadToggleButton for command of this type.

    2. It looks like you added your custom command to the BingMapProvider. In this case you should restore a state of your command when the mode of BingMapProvider is changed.

    The sample code is below.

    <Window x:Class="CustomCommandExample.MainWindow"
            xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"     
            Title="MainWindow" Height="350" Width="525">
        <Grid x:Name="LayoutRoot">
            <Grid.Resources>
                <DataTemplate x:Key="CustomCommandDataTemplate">
                    <telerik:RadToggleButton Command="{Binding Path=Command}"
                                    CommandParameter="{Binding CommandParameter}"
                                    IsChecked="{Binding Path=IsSelected, Mode=TwoWay}"
                                    HorizontalContentAlignment="Stretch"
                                    Height="26"
                                    Margin="0,3"
                                    CornerRadius="3"
                                    Opacity="0.8"
                                    BorderBrush="#FF848484">
                        <telerik:RadToggleButton.Background>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Color="White"/>
                                <GradientStop Color="#FFD4D4D4" Offset="1"/>
                                <GradientStop Color="Gainsboro" Offset="0.5"/>
                                <GradientStop Color="#FFADADAD" Offset="0.51"/>
                            </LinearGradientBrush>
                        </telerik:RadToggleButton.Background>
                        <Grid HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition />
                                <ColumnDefinition Width="32" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding Path=Command.Text}"
                                  TextAlignment="Center"
                                  HorizontalAlignment="Stretch"
                                  Padding="7,0"
                                  FontSize="11"
                                  FontWeight="Bold" />
                            <Image Grid.Column="1" Width="16" Height="16" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Image.Source>
                                    <BitmapImage UriSource="{Binding ImageUri}" />
                                </Image.Source>
                            </Image>
                        </Grid>
                    </telerik:RadToggleButton>
                </DataTemplate>
            </Grid.Resources>
            <telerik:RadMap x:Name="RadMap1">
            </telerik:RadMap>
        </Grid>
    </Window>

     

    public partial class MainWindow : Window
    {
        private string VEKey = "xxx-yyy-zzz";
        private BingMapProvider provider;
        private bool isShowWMSSelected = false;
        private CommandDescription commandDescription;
         
        public MainWindow()
        {
            InitializeComponent();
     
            this.SetProvider();
        }
     
        private void SetProvider()
        {
            this.provider = new BingMapProvider(MapMode.Aerial, true, this.VEKey);
            this.provider.MapSourceChanged += provider_MapSourceChanged;
     
            this.AddCustomCommandAction(this.provider);
            this.RadMap1.Provider = provider;
        }
     
        void provider_MapSourceChanged(object sender, MapSourceChangedEventArgs e)
        {
            commandDescription.IsSelected = isShowWMSSelected;
        }
     
        private void AddCustomCommandAction(BingMapProvider provider)
        {
            string commandText = "Show WMS";
            string commandName = "ShowWMS";
            this.commandDescription = new CommandDescription();
            this.commandDescription.Command = new RoutedUICommand(commandText, commandName, typeof(BingMapProvider));
            this.commandDescription.DataTemplate = this.LayoutRoot.Resources["CustomCommandDataTemplate"] as DataTemplate;
     
            this.commandDescription.ImageUri = new Uri("/CustomCommandExample;component/Resources/Images/CommandImage.png", UriKind.RelativeOrAbsolute);
            CommandBinding commandBinding = new CommandBinding(commandDescription.Command, ExecuteCustomCommand);
            provider.Commands.Add(this.commandDescription);
            provider.CommandBindingCollection.Add(commandBinding);
        }
     
        private void ExecuteCustomCommand(object sender, ExecutedRoutedEventArgs e)
        {
            this.isShowWMSSelected = !this.isShowWMSSelected;
     
            if (this.isShowWMSSelected)
            {
                // Add WMS provider
            }
            else
            {
                // Remove WMS provider
            }
        }
    }

    Regards,
    Andrey Murzov
    Telerik

    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Jose Ramon
    Jose Ramon avatar
    2 posts
    Member since:
    Sep 2013

    Posted 26 Sep 2013 Link to this post

    Thanks for your answer!!!! It is very helpful!
Back to Top