Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't tell what you are doing wrong without seeing your code. Below is a simple usercontrol that allows users to pick a color. </p> <pre><code>&lt;UserControl x:Class="ColorPickerTest.ColorPicker" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;ToggleButton Name="redButton" Content="Red" Click="Button_Click" /&gt; &lt;ToggleButton Name="yellowButton" Content="Yellow" Click="Button_Click" /&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace ColorPickerTest { public partial class ColorPicker : UserControl { public ColorPicker() { InitializeComponent(); } public Brush SelectedColor { get { return (Brush)GetValue(SelectedColorProperty); } set { SetValue(SelectedColorProperty, value); } } public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(Brush), typeof(ColorPicker), new UIPropertyMetadata(Brushes.Transparent, OnPropertyChanged)); private void Button_Click(object sender, RoutedEventArgs e) { if (!redButton.IsChecked.GetValueOrDefault() &amp;&amp; !yellowButton.IsChecked.GetValueOrDefault()) { SelectedColor = Brushes.Transparent; } else if (!redButton.IsChecked.GetValueOrDefault() &amp;&amp; yellowButton.IsChecked.GetValueOrDefault()) { SelectedColor = Brushes.Yellow; } else if (redButton.IsChecked.GetValueOrDefault() &amp;&amp; !yellowButton.IsChecked.GetValueOrDefault()) { SelectedColor = Brushes.Red; } else { // redButton.IsChecked.GetValueOrDefault() &amp;&amp; yellowButton.IsChecked.GetValueOrDefault()) SelectedColor = Brushes.Orange; } } private static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs e) { ColorPicker colorPicker = sender as ColorPicker; colorPicker.redButton.IsChecked = colorPicker.SelectedColor == Brushes.Red || colorPicker.SelectedColor == Brushes.Orange; colorPicker.yellowButton.IsChecked = colorPicker.SelectedColor == Brushes.Yellow || colorPicker.SelectedColor == Brushes.Orange; } } } &lt;Window x:Class="ColorPickerTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ColorPickerTest="clr-namespace:ColorPickerTest" Height="300" Width="300"&gt; &lt;StackPanel&gt; &lt;ColorPickerTest:ColorPicker SelectedColor="{Binding Path=MyColor, Mode=TwoWay}" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; using System.Windows; using System.Windows.Media; namespace ColorPickerTest { public partial class Window1 : Window { public Window1() { InitializeComponent(); MyColor = Brushes.Red; DataContext = this; } private Brush _myColor; public Brush MyColor { get { return _myColor; } set { _myColor = value; Background = _myColor; } } } } </code></pre>
 

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