Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I Bind a viewmodel property to a dependency property of a custom control
    text
    copied!<p>I want to use a color picker in my wpf application and I saw a nice looking one on <a href="http://www.codeproject.com/KB/miscctrl/WPFColorPicker.aspx" rel="nofollow noreferrer">this codeproject</a> page. The control works fine until I want to connect the control to a viewmodel. I created a small test program with this viewmodel:</p> <pre><code>public class ColorViewModel : ViewModelBase { public ColorViewModel() { LineColor = Brushes.Yellow; } SolidColorBrush _brushColor; public SolidColorBrush LineColor { get { return _brushColor; } set { _brushColor = value; RaisePropertyChanged(() =&gt; LineColor); } } } </code></pre> <p>The test program has a textbox and the colorpicker controls:</p> <pre><code>&lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="Please Select a Color" FontWeight="Bold" Margin="10" Foreground="{Binding Path=LineColor, UpdateSourceTrigger=PropertyChanged}"/&gt; &lt;vw:ColorPickerControlView x:Name="ForeColorPicker" Margin="10" CurrentColor="{Binding Path=LineColor, UpdateSourceTrigger=PropertyChanged }"/&gt; &lt;/StackPanel&gt; </code></pre> <p>In the loaded event of the main window in my test application I set the viewmodel to the datacontext like this:</p> <pre><code> DataContext = new ColorViewModel(); </code></pre> <p>The problem is that I can't seem to bind the LineColor property of the viewmodel to the CurrentColor property of the ColorPickerControlView. The CurrentControl property of the ColorPickerControlView seems to be fine. The constructor looks like this:</p> <pre><code>public ColorPickerControlView() { this.DataContext = this; InitializeComponent(); CommandBindings.Add(new CommandBinding(SelectColorCommand, SelectColorCommandExecute)); } </code></pre> <p>In the constructor of the UserControl there is the line this.DataContext = this; I read that is is necessary to bind the dependency properties. Do I override this line when I set my viewmodel to the datacontext and is that why I can't bind to the CurrentColor property? Is there any workaround? Or did I make another mistake?</p>
 

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