Bodul
Bodul

Reputation: 185

Assign Button color during creation of button (runtime)

First I want to appologize for so much code, but English is not my native language so I put as much details as I could, and hope that you will understand what my problem is.

Also I just started learning C# 20 days ago so my error will probably be some basic newbie error :)

Anyway, I have WPF form with few Grids, and in one of the Grid there is:


<Grid  Grid.Column="2" Grid.Row="1" Name="grdPLUPanel"  >
            <ItemsControl x:Name="btnPLUList">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="4" Margin="0"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content ="{Binding Content}"  Height="{Binding Height}" Width="{Binding Width}" Tag="{Binding Tag}" Margin="{Binding Margin}"  Background="{Binding Color}" FontSize="{Binding FontSize}" FontWeight="Medium" HorizontalAlignment="Center"  Click="ClickHandlerGrp" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>

All of those Bindings Works perfectly except for Background="{Binding Color}".


Code for assigning color which I am getting from database as int (ie. -32768), then converting to Hex(#FFFF8000) and adding to the Background is:

   if (dictPLU.ContainsKey(Convert.ToString(i)))
            {
                GetPLURowsFromDB.MyObject valuePLU = dictPLU[Convert.ToString(i)];
                byte[] bytes = BitConverter.GetBytes(valuePLU.btnColor);
                var newColor = new SolidColorBrush(Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]));

                btns.Add(new TodoItem() { Content = valuePLU.btnContent, Height = btnMinimumHeightSize, Width = btnMinimumWidthSize, FontSize = fntSize, Tag = valuePLU.btnPLUID, Background = newColor, Margin = "1," + separationX + ",0,0" });
            }
            else
            {
                btns.Add(new TodoItem() { Content = "", Height = btnMinimumHeightSize, Width = btnMinimumWidthSize, Tag = "PLU" + Convert.ToString(i), Margin = "1," + separationX + ",0,0" });
            }

Above code doesn't work, also there are no errors, Button Background simply does not change. When debugging:

newColor is (#FFFF8000)

valuePLU is (-32768)

Background is {#FFFFFFFF} - a default color assigned automatically when buttons are created.


However if i put Button (btnRcptESC) manually on the form, and use following code:

 private void MainWindowView_OnLoaded(object sender, RoutedEventArgs e)
    {

        byte[] bytes = BitConverter.GetBytes(Convert.ToInt32("-32768"));
        var colorNew = new SolidColorBrush(Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]));
        btnRcptESC.Background = colorNew;
    }

Button Will Change Color.

I am suspecting that problem is in Constructor "public SolidColorBrush Background { get; set; }" Maybe SolidColorBrush is not proper type?

Upvotes: 0

Views: 80

Answers (1)

Bodul
Bodul

Reputation: 185

I've figured out, the problem was in XAML (Background="{Binding Color}" ) and Constructor (public SolidColorBrush Background { get; set; }).

For this to work correctly, in XAML should be (Background="{Binding Background})"

As I said, this is probably noob error :)

Thank you all!

Upvotes: 1

Related Questions