0

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?

4
  • your English is OK! otherwise can you clarify this a little, you try to add buttons dynamically to the form. but where is the relation to the static button you have in your XAML? The color of what doesn't change? the static button or the dynamically created buttons come out having the wrong color? Commented Oct 20, 2018 at 10:07
  • Static Button is just to test that part of code for conversion from int to hex, and that button changes color. I am having problem with changing color on button while creating them dynamically. There are no errors, they simply ignore newColor and retain default color. Commented Oct 20, 2018 at 10:19
  • @Bodul It is not possible to close the question unless you write an anwer - you can just take the text from update and answer your question yourself and then accept as solution Commented Oct 20, 2018 at 11:26
  • Ahh ok thanks! Will do that. Commented Oct 20, 2018 at 11:30

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Good job, the property name what you have declared in your ViewModel should be used to bind to UI. Agreed its a beginner mistake :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.