Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

If I place CommandBindings in a parent container control, my command ceases working

$
0
0

Hi there:

I implemented ICommandSource on a Slider, which I named CommandedSlider:

public class CommandedSlider : Slider, ICommandSource {

    // This is the implementation of ICommandSource
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register(
            "SliderCommand",
            typeof(ICommand),
            typeof(CommandedSlider),
            new PropertyMetadata((ICommand)null, new PropertyChangedCallback(CommandChangedCallback)));
    public ICommand Command {        // ICommandSource member
        get {
            return (ICommand)GetValue(CommandProperty);
        }
        set {
            SetValue(CommandProperty, value);
        }
    }
...
...



My xaml declaration doesn't specify CommandTarget since I want it's command it to operate on the currently focused TextBox:

<local:CommandedSlider x:Name="FontSlider" Background="AliceBlue" Minimum="0" Grid.Row="0"
            Maximum="40"
            TickFrequency="5"
            Height="40" TickPlacement="BottomRight"
            LargeChange="5"
            SmallChange="5"
            Command="{x:Static local:MainWindow.FontUpdateCommand}"
            CommandParameter="{Binding ElementName=FontSlider,
                                        Path=Value,
                                        Converter={StaticResource DoubleToIntConverterResource}}"
            Focusable="False" /><StackPanel Grid.Row="1" Margin="0,3,0,0"><TextBox Height="66" Name="textBox1" Width="179" ><TextBox.CommandBindings><CommandBinding Command="{x:Static local:MainWindow.FontUpdateCommand}"
                    Executed="SliderUpdateExecuted"
                    CanExecute="SliderUpdateCanExecute" /></TextBox.CommandBindings>
        This is a text</TextBox><TextBox Height="56" Name="textBox2" Width="181" ><TextBox.CommandBindings><CommandBinding Command="{x:Static local:MainWindow.FontUpdateCommand}"
                    Executed="SliderUpdateExecuted"
                    CanExecute="SliderUpdateCanExecute" /></TextBox.CommandBindings>
        This is another text</TextBox></StackPanel>

Notice that I declare the same command binding on both TextBoxes, but If I take them off the TextBoxes and put it at the parent StackPanel, like so:

<StackPanel Grid.Row="1" Margin="0,3,0,0"><StackPanel.CommandBindings><CommandBinding Command="{x:Static local:MainWindow.FontUpdateCommand}"
                Executed="SliderUpdateExecuted"
                CanExecute="SliderUpdateCanExecute" /></StackPanel.CommandBindings><TextBox Height="66" Name="textBox1" Width="179" >
        This is a text</TextBox><TextBox Height="56" Name="textBox2" Width="181" >
        This is another text</TextBox></StackPanel>


Then the command won't work at all. Why?, I want to declare it at the containing parent level so I can do it only once.

Rafael


Believe you can do it, and you will!!



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>