Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Unable to get commands and dependency properties to work together, or am i just using them wrong?
    primarykey
    data
    text
    <p>I have created a wpf application into which I have added a user control and a custom control. The custom control is used within the user control (planned to be used in many other Types of user controls). </p> <p>For a quick example, the custom control has a dependency property of type <code>Brush</code> titled backgroundcolour, This is then set in the defualt template for the custom control as the background for the border. My thinking was that I would be able to set this value from the user control using the <code>Command</code> and <code>CommandParameter</code> properties. As i tried to do below</p> <p>User Control xaml (TestControl is the custom control)</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;MyNamespace:EditColourCommand x:Key="EditColour"/&gt; &lt;/Grid.Resources&gt; &lt;Grid.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Name="Test" Header="Test" Command="{StaticResource EditColour}" CommandParameter="{Binding ElementName=testControl1, Path=BackgroundColour, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;/ContextMenu&gt; &lt;/Grid.ContextMenu&gt; &lt;MyNamespace:TestControl HorizontalAlignment="Left" Margin="213,90,0,0" x:Name="testControl1" VerticalAlignment="Top" Height="77" Width="230"/&gt; &lt;/Grid&gt; </code></pre> <p>My custom Control Code Behind:</p> <pre><code>public class TestControl : Control { static TestControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl))); } public static DependencyProperty BackgroundColourProperty = DependencyProperty.Register("BackgroundColour", typeof(Brush), typeof(TestControl), new PropertyMetadata(Brushes.Blue, BackgroundColourPropertyChangedHandler)); public Brush BackgroundColour { get { return (Brush)GetValue(BackgroundColourProperty); } set { SetValue(BackgroundColourProperty, value); } } public static void BackgroundColourPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs e) { } } </code></pre> <p>Finally, The Command</p> <pre><code>public class EditColourCommand : ICommand { public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { parameter = Brushes.Black; } } </code></pre> <p>The command gets fired and it has the defualt value in this case blue as a parameter but it never sets the value to black. Can someone push me in the right direction please?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload