title: Unable to change background colour for new button
body: <p>I am using XAML and MVVM pattern for an application.</p>
  
  <p>In part of my application I have a window split into 2 parts, a left column and right hand column/area</p>
  
  <p>In the left column are a row of buttons ( generated on loading the window ) </p>
  
  <p>The right column/area will display a group of buttons depending on which button the user clicks in the left column, so for example say I have 4 department buttons in the left column, each department button will have a different number of stock items.</p>
  
  <p>If I click button 1 in the left column, I do some code in the viewmodel to fetch the names of items in that department. And then I build a new observableCollection to display in the right column/area.</p>
  
  <p>So this isnt a problem, the right amount of buttons get generated each time ok.
  However when I try to change the background colour dynamically in the ViewModel, the colour is NOT upodated in the view. </p>
  
  <p>The strange thing is, I can change the content, forecolour and other properties but just not the background colour. It appears the background will only generate correctly if loaded and run time. I cant change it otherwise while using the window.</p>
  
  <p>I have tried Brushes, creating and assigning new style and even clearing the dependancy property of the button ( .ClearValue(Button.BackgroundProperty) )</p>
  
  <p>Would any one know how i can get the background to change colour while the window is open and when i want to generate a set of buttons dynamically in my viewmodel?</p>
  
  <p>Many thanks all... I have attached my XAML and C# snippet, the </p>
  
  <p>XAML : 
          
            
          
              
                  
              
          
          </p>
  
  <pre><code>&lt;dxd:DockLayoutManager Name="dlSalesScreen"&gt;
      &lt;dxd:DockLayoutManager.LayoutRoot&gt;
          &lt;dxd:LayoutGroup Name="Root" Orientation="Horizontal" AllowSplitters="False"&gt;
  
              &lt;dxd:LayoutPanel AllowClose="False" AllowRename="False" 
                               Caption="Departments" HorizontalScrollBarVisibility="Hidden" 
                               CaptionAlignMode="AutoSize"
                               CaptionImageLocation="BeforeText" ShowPinButton="False" &gt;
  
                  &lt;!-- Scrollviewer for department buttons--&gt;
                  &lt;ScrollViewer x:Name="deptScrollviewer" Grid.Row="0" Grid.Column="0" Width="185" Height="Auto" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" PanningMode="VerticalOnly"&gt;
                      &lt;ItemsControl ItemsSource="{Binding Departments}"&gt;
                          &lt;ItemsControl.ItemsPanel&gt;
                              &lt;ItemsPanelTemplate&gt;
                                  &lt;WrapPanel VerticalAlignment="Top" Height="Auto" Orientation="Vertical" /&gt;
                              &lt;/ItemsPanelTemplate&gt;
                          &lt;/ItemsControl.ItemsPanel&gt;
                      &lt;/ItemsControl&gt;
                  &lt;/ScrollViewer&gt;
              &lt;/dxd:LayoutPanel&gt;
  
              &lt;dxd:LayoutPanel AllowClose="False" AllowRename="False" 
                               Caption="Available stock in department" Width="Auto" 
                               CaptionAlignMode="AutoSize"
                               CaptionImageLocation="BeforeText"  ShowPinButton="False"&gt;
                  &lt;Grid&gt;
                      &lt;Grid.RowDefinitions&gt;
                          &lt;RowDefinition Height="*" /&gt;
                          &lt;RowDefinition Height="50" /&gt;
                      &lt;/Grid.RowDefinitions&gt;
                      &lt;!-- Scrollviewer for stock buttons--&gt;
                      &lt;ScrollViewer x:Name="stockScrollViewer" Grid.Row="0" Width="Auto" Height="Auto" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PanningMode="VerticalOnly"&gt;
                          &lt;ItemsControl ItemsSource="{Binding StockItems, UpdateSourceTrigger=PropertyChanged}"&gt;
                              &lt;ItemsControl.ItemsPanel&gt;
                                  &lt;ItemsPanelTemplate&gt;
                                      &lt;WrapPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Horizontal" Width="400" /&gt;
                                  &lt;/ItemsPanelTemplate&gt;
                              &lt;/ItemsControl.ItemsPanel&gt;
                          &lt;/ItemsControl&gt;
                      &lt;/ScrollViewer&gt;
                      &lt;Rectangle Grid.Row="1" Margin="0,0,0,0" Height="Auto" Fill="{StaticResource BottomRectangleGradient}" /&gt;
                      &lt;Grid Name="gridButtonHolder" Grid.Row="2"&gt;
                          &lt;Grid.ColumnDefinitions&gt;
                              &lt;ColumnDefinition Width="Auto" /&gt;
                              &lt;ColumnDefinition Width="Auto" /&gt;
                              &lt;ColumnDefinition Width="Auto" /&gt;
                          &lt;/Grid.ColumnDefinitions&gt;
                          &lt;GroupBox x:Name="grpStockItem" Grid.Column="0" Header="Selected Item" HorizontalAlignment="Left" Width="200" &gt;
                              &lt;Label x:Name="lblStockName" Content="{Binding SelectedStockItemLabel}" HorizontalAlignment="Left" /&gt;
                          &lt;/GroupBox&gt;
                          &lt;Button Name="btnSave" Content="Apply" Command="{Binding ConfirmSelectionCommand}" dxc:ThemeManager.ThemeName="Office2007Blue" Grid.Column="1" Width="110" Height="42" Margin="0,8,0,0" /&gt;
                          &lt;Button Name="btnClose" Content="Cancel" dxc:ThemeManager.ThemeName="Office2007Blue" Grid.Column="2" Width="110" Height="42" Margin="0,8,0,0" /&gt;
                      &lt;/Grid&gt;
                  &lt;/Grid&gt;
              &lt;/dxd:LayoutPanel&gt;
          &lt;/dxd:LayoutGroup&gt;
      &lt;/dxd:DockLayoutManager.LayoutRoot&gt;
  &lt;/dxd:DockLayoutManager&gt;
  </code></pre>
  
  <p></p>
  
  <p>C#</p>
  
  <pre><code> void DeptClicked(object sender, RoutedEventArgs e)
      {
          SelectedDeptID = Convert.ToInt32(((Button)sender).Tag.ToString());
  
          _stockButtons = new ObservableCollection&lt;Button&gt;();
  
          if (StockItemCount &gt; 0)
          {
              for (int i = 0; i &lt; StockItemCount; i++)
              {
                  //_stockButtons.Add(new Button());
                  Button btn = new Button();
                  btn.Background = Brushes.Aquamarine;
  
                  btn.Height = 100;
                  btn.Width = 100;
                  btn.Content = i.ToString();
  
                  _stockButtons.Add(btn);
              }
          }
  
          RaisePropertyChanged("StockItems");
      }
  
   public ObservableCollection&lt;Button&gt; Departments
      {
          get
          {
              if (_deptButtons == null)
              {
                  _deptButtons = new ObservableCollection&lt;Button&gt;();
  
                  for (int i = 0; i &lt; DeptCount; i++)
                  {
                      Button button = new Button();
                      button.Content = DepartmentNames[i];
                      button.Tag = DepartmentIDs[i].ToString();
                      button.Click += new RoutedEventHandler(DeptClicked);
  
                      button.Width = 128;
                      button.Height = 100;
  
                      _deptButtons.Add(button);
                  }
  
              }
              return _deptButtons;
          }
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/eX9c4.png" alt="enter image description here"></p>