1
votes

I have stackpanel to which I am adding RadioButtons dynamically as children. Radiobuttons have content which is integer.

I also have Y x Y grid (size determined from code dynamically), to which I'm adding dynamically Buttons and allow user to change Button's content into string which represents integer number.

Here is where I need help:

After checking arbitrary radiobutton from stackpanel, I'd like all buttons from grid that have same number to have their background color changed.

As I am new to WPF I am not sure how to achieve this and your help would be greatly appreciated.

EDIT: I made a little progress, what I do is basically bind Button.Content with RadioButton.IsChecked and RadioButton.Content for everybutton and every radiobutton, but I have problem that it only works for last radiobutton here is code (rbuts=parent control of radiobuttons, MyGrid=parent control of buttons):

for (int z = 0; z < boardSize * boardSize; z++)
        {

            Button b1 = MyGrid.Children[z] as Button;
            for (int i = 0; i < boardSize; i++)
            {
                MultiBinding rbtnBinding = new MultiBinding();
                rbtnBinding.Converter = new RadioButtonHighlightConverter();
                rbtnBinding.Bindings.Add(new Binding("IsChecked") { Source = rbuts.Children[i] });
                rbtnBinding.Bindings.Add(new Binding("Content") { Source = rbuts.Children[i] });
                rbtnBinding.Bindings.Add(new Binding("Content") { Source = MyGrid.Children[z] });
                rbtnBinding.NotifyOnSourceUpdated = true;
                b1.SetBinding(Button.BackgroundProperty, rbtnBinding);

            }

        }

Its as if I cannot set many different multibindings for same button...

1
The answer from Use a Style Selector for a Button would probably resolve your issue. - Chiune Sugihara

1 Answers

0
votes

You could place your Button Styles in a Resource Dictionary and bind the Style for the Button and use a Converter

ButtonStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Background" Value="Green"/>
        <Setter Property="FontSize" Value="12"/>
    </Style>
    <Style x:Key="ButtonStyle2" TargetType="Button">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="FontSize" Value="14"/>
    </Style>
</ResourceDictionary>

Then for the Button that has this requirement you bind Style to the property of interest

<Button ...
        Style="{Binding Path=MyDataProperty,
                        Converter={StaticResource ButtonStyleConverter}}"/>

And in the Converter you load the ButtonStyles Resource Dictionary and return the desired Style based on the value

public class ButtonStyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Uri resourceLocater = new Uri("/YourNameSpace;component/ButtonStyles.xaml", System.UriKind.Relative);
        ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
        if (value.ToString() == "Some Value")
        {
            return resourceDictionary["ButtonStyle1"] as Style;
        }
        return resourceDictionary["ButtonStyle2"] as Style;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

EDIT :

Added details for converter parameter

<RadioButton Content="None"
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <RadioButton.IsChecked>
        <Binding Path="MyProperty"
                 Converter="{StaticResource IntToBoolConverter}">
            <Binding.ConverterParameter>
                <sys:Int32>0</sys:Int32>
            </Binding.ConverterParameter>
        </Binding>
    </RadioButton.IsChecked>
</RadioButton>