无法更改新按钮的背景颜色

时间:2012-09-19 16:41:55

标签: c# wpf xaml devexpress

我正在为应用程序使用XAML和MVVM模式。

在我的应用程序的一部分中,我有一个分为两部分的窗口,一个左列和右列/区域

在左栏中有一排按钮(在加载窗口时生成)

右栏/区域将显示一组按钮,具体取决于用户在左栏中单击的按钮,例如说我左栏中有4个部门按钮,每个部门按钮将具有不同数量的库存项目

如果我点击左栏中的按钮1,我会在viewmodel中执行一些代码来获取该部门中的项目名称。然后我构建一个新的observableCollection以显示在右列/区域中。

所以这不是问题,每次都可以生成适量的按钮。 但是,当我尝试在ViewModel中动态更改背景颜色时,颜色不会在视图中上调。

奇怪的是,我可以改变内容,前色和其他属性,但不能改变背景颜色。如果加载和运行时间,背景似乎只会正确生成。在使用窗口时,我无法改变它。

我尝试过刷子,创建和分配新样式,甚至清除按钮的依赖属性(.ClearValue(Button.BackgroundProperty))

当窗口打开以及我想在我的视图模型中动态生成一组按钮时,是否有人知道如何让背景改变颜色?

非常感谢所有人......我附上了我的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

1 个答案:

答案 0 :(得分: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。