Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding on background color with a custom style on a button and checkbox
    text
    copied!<p>I am currently working on a WPF application and have redefined the buttons and checkbox to use a custom style in App.xaml.</p> <p><strong>App.xaml</strong></p> <pre class="lang-xml prettyprint-override"><code>&lt;Style x:Key="{x:Type CheckBox}" TargetType="CheckBox"&gt; &lt;Setter Property="Margin" Value="0,0,10,5" /&gt; &lt;Setter Property="Width" Value="85" /&gt; &lt;Setter Property="Height" Value="50" /&gt; &lt;Setter Property="SnapsToDevicePixels" Value="true" /&gt; &lt;Setter Property="OverridesDefaultStyle" Value="true" /&gt; &lt;Setter Property="FocusVisualStyle" Value="{DynamicResource CheckBoxFocusVisual}" /&gt; &lt;Setter Property="Template" /&gt; &lt;Setter.Value /&gt; &lt;!-- ... etc. --&gt; &lt;/Style&gt; </code></pre> <p>The code is from <a href="http://msdn.microsoft.com/en-us/library/ms752319%28v=vs.85%29.aspx" rel="nofollow">here</a> with some modifications.</p> <p>Now I want to change the background color through coding. For example, if the title of the checkbox is "a" then the background color would be red and so on. I succeeded in binding the titles but not the background color. My checkboxes are used inside a control item.</p> <p><strong>MainWindow.xaml</strong></p> <pre class="lang-xml prettyprint-override"><code>&lt;Grid&gt; &lt;ItemsControl Name="ControllerDisplay"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;WrapPanel HorizontalAlignment="Center"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding controltitle}" Style="{StaticResource MainControltitle}"/&gt; &lt;CheckBox&gt; // want to change checkbox background &lt;CheckBox.Style&gt; &lt;Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}"&gt; &lt;Setter Property="Background" Value="Aqua" /&gt; &lt;/Style&gt; &lt;/CheckBox.Style&gt; &lt;StackPanel &gt; &lt;TextBlock Text="{Binding controlno}" HorizontalAlignment="Center" FontSize="16" /&gt; &lt;TextBlock Text="{Binding status}" HorizontalAlignment="Center" FontSize="10"/&gt; &lt;/StackPanel&gt; &lt;/CheckBox&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/Grid&gt; </code></pre> <p><strong>on the coding side:</strong></p> <pre class="lang-cs prettyprint-override"><code>public void printcheckbox() { List&lt;Controller&gt; listItem = new List&lt;Controller&gt;(); listItem.Add(new Controller() { controltitle = "title1", controlno = "12345", status = "On" , controlbg = Colors.Red}); ControllerDisplay.ItemsSource = listItem; } public class Controller { public string controltitle { get; set; } public string controlno { get; set; } public string status { get; set; } public Color controlbg { get; set; } } </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