2012-09-19 30 views
1

我正在爲應用程序使用XAML和MVVM模式。無法更改新按鈕的背景顏色

在我的應用程序的一部分,我有一個窗口分成2份,左柱和右邊的列/區域

在左側列是一排按鈕(上裝載窗口生成)

右欄/區域將顯示一組按鈕,具體取決於用戶在左欄中單擊的按鈕,例如,假設我在左欄中有4個部門按鈕,則每個部門按鈕的庫存件數不同。

如果我點擊左欄中的按鈕1,我會在viewmodel中執行一些代碼來獲取該部門中的項目名稱。然後我構建一個新的observableCollection,以顯示在右欄/區域中。

所以這不是一個問題,每次都可以生成正確數量的按鈕。 但是,當我嘗試在ViewModel中動態更改背景顏色時,顏色不在視圖中更新。

奇怪的是,我可以改變內容,前景和其他屬性,但不是背景顏色。它看起來只有在加載和運行時才能正確生成背景。在使用窗口時我無法改變它。

我已經試過刷,創建和分配新的風格,甚至清除按鈕(.ClearValue(Button.BackgroundProperty))的扶養財產

會不會有人知道我能得到的背景來改變顏色,而窗口是打開的,當我想在viewmodel中動態生成一組按鈕時?

非常感謝所有......我重視我的XAML和C#代碼段,該

XAML:

<dxd:DockLayoutManager Name="dlSalesScreen"> 
    <dxd:DockLayoutManager.LayoutRoot> 
     <dxd:LayoutGroup Name="Root" Orientation="Horizontal" AllowSplitters="False"> 

      <dxd:LayoutPanel AllowClose="False" AllowRename="False" 
          Caption="Departments" HorizontalScrollBarVisibility="Hidden" 
          CaptionAlignMode="AutoSize" 
          CaptionImageLocation="BeforeText" ShowPinButton="False" > 

       <!-- Scrollviewer for department buttons--> 
       <ScrollViewer x:Name="deptScrollviewer" Grid.Row="0" Grid.Column="0" Width="185" Height="Auto" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" PanningMode="VerticalOnly"> 
        <ItemsControl ItemsSource="{Binding Departments}"> 
         <ItemsControl.ItemsPanel> 
          <ItemsPanelTemplate> 
           <WrapPanel VerticalAlignment="Top" Height="Auto" Orientation="Vertical" /> 
          </ItemsPanelTemplate> 
         </ItemsControl.ItemsPanel> 
        </ItemsControl> 
       </ScrollViewer> 
      </dxd:LayoutPanel> 

      <dxd:LayoutPanel AllowClose="False" AllowRename="False" 
          Caption="Available stock in department" Width="Auto" 
          CaptionAlignMode="AutoSize" 
          CaptionImageLocation="BeforeText" ShowPinButton="False"> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="*" /> 
         <RowDefinition Height="50" /> 
        </Grid.RowDefinitions> 
        <!-- Scrollviewer for stock buttons--> 
        <ScrollViewer x:Name="stockScrollViewer" Grid.Row="0" Width="Auto" Height="Auto" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" PanningMode="VerticalOnly"> 
         <ItemsControl ItemsSource="{Binding StockItems, UpdateSourceTrigger=PropertyChanged}"> 
          <ItemsControl.ItemsPanel> 
           <ItemsPanelTemplate> 
            <WrapPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Horizontal" Width="400" /> 
           </ItemsPanelTemplate> 
          </ItemsControl.ItemsPanel> 
         </ItemsControl> 
        </ScrollViewer> 
        <Rectangle Grid.Row="1" Margin="0,0,0,0" Height="Auto" Fill="{StaticResource BottomRectangleGradient}" /> 
        <Grid Name="gridButtonHolder" Grid.Row="2"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto" /> 
          <ColumnDefinition Width="Auto" /> 
          <ColumnDefinition Width="Auto" /> 
         </Grid.ColumnDefinitions> 
         <GroupBox x:Name="grpStockItem" Grid.Column="0" Header="Selected Item" HorizontalAlignment="Left" Width="200" > 
          <Label x:Name="lblStockName" Content="{Binding SelectedStockItemLabel}" HorizontalAlignment="Left" /> 
         </GroupBox> 
         <Button Name="btnSave" Content="Apply" Command="{Binding ConfirmSelectionCommand}" dxc:ThemeManager.ThemeName="Office2007Blue" Grid.Column="1" Width="110" Height="42" Margin="0,8,0,0" /> 
         <Button Name="btnClose" Content="Cancel" dxc:ThemeManager.ThemeName="Office2007Blue" Grid.Column="2" Width="110" Height="42" Margin="0,8,0,0" /> 
        </Grid> 
       </Grid> 
      </dxd:LayoutPanel> 
     </dxd:LayoutGroup> 
    </dxd:DockLayoutManager.LayoutRoot> 
</dxd:DockLayoutManager> 

C#

void DeptClicked(object sender, RoutedEventArgs e) 
    { 
     SelectedDeptID = Convert.ToInt32(((Button)sender).Tag.ToString()); 

     _stockButtons = new ObservableCollection<Button>(); 

     if (StockItemCount > 0) 
     { 
      for (int i = 0; i < 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<Button> Departments 
    { 
     get 
     { 
      if (_deptButtons == null) 
      { 
       _deptButtons = new ObservableCollection<Button>(); 

       for (int i = 0; i < 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; 
     } 
    } 

enter image description here

+0

我對DevExpress控件並不熟悉,但看起來您的新按鈕具有與「應用」和「取消」按鈕具有相同的「Office2007Blue」主題。也許DevExpress主題管理器接管按鈕的背景。 – dcstraw

回答

1

嘗試類似的東西:

Button btn = new Button(); 
btn.Background = Brushes.Green;  
btn.Height = 100; btn.Width = 100; 
btn.Content = i.ToString(); 

ThemeManager.SetThemeName(btn, "None"); 

_stockButtons.Add(btn); 

類ThemeMagager在命名空間DevExpress.Xpf.Core。

+0

嗨Kmatyaszek ... THANKKK YOUUUUUUUUUUUUUUUUUUUU !!!!!!!!你是天才!不能感謝你足夠的...花了2天,在這個字面上撓我的頭!再次感謝:) – Kev

+0

不客氣:) – kmatyaszek