czioutas
czioutas

Reputation: 1068

Make a Button look like an Ellipse and then Bind the Color of it

This is my XAML makiing a Style where the Button looks like an Ellipse. I had also made the color with a key so i can change or even bind it(not with any luck). The problem with the binding is that i dont have a Name to bind with the DataContent.

    <Color x:Key="Player1C" >Gray</Color>


    <Style x:Key="Player11" x:Name="Pl1style" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Ellipse Name="EllipsePl1" Width="30" Height="30" Stroke="#FF000000" StrokeThickness="1">
                            <Ellipse.Fill>
                                <SolidColorBrush Color="{StaticResource Player1C}"/>
                            </Ellipse.Fill>
                        </Ellipse>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I have tried to do this in C# Player1 = new Style(typeof(Button)); Player1.Setters.Add(new Setter(Ellipse.FillProperty, new SolidColorBrush(GameInstance.Color1)));

But i just get a Button

I then created a ControlTemplate , a grid ,an ellipse and filled the ellipse in order to copy exactly the XAML

            ControlTemplate ct1 = new ControlTemplate(typeof(Button));
        Grid agrid = new Grid();
        Ellipse el1 = new Ellipse();
        el1.Fill = new SolidColorBrush(Colors.Black);

But i get error when i try to do Player1.Setters.Add(new Setter(ct1 etc etc)

Any help will be much appreciated , either a Binding with the XAML or a way to do it in c sharp. I have also tried creating my own class of button but had again problems.

Upvotes: 0

Views: 6684

Answers (2)

Jonas
Jonas

Reputation: 675

You can bind the fill color of ellipse to the background of the button. So you simply have to change the background of the button.

Template :

<Ellipse Fill="{TemplateBinding Background}"/>

Upvotes: 2

yo chauhan
yo chauhan

Reputation: 12295

try this

<Style x:Key="Player11" x:Name="Pl1style" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Ellipse Name="EllipsePl1" Width="30" Height="30" Stroke="#FF000000" StrokeThickness="1">
                            <Ellipse.Fill>
                                <SolidColorBrush Color="{DynamicResource Player1C}"/>
                            </Ellipse.Fill>
                        </Ellipse>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

public MainWindow()
    {
         InitializeComponent();
         this.Resources.Add("Player1C",Colors.Black);
         DataContext = this;

    }


    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.Resources["Player1C"] = Colors.Red;
    }

This is one of the many ways you can do in c# .However you can also do it using Trigger. Using Binding You can do it this way.

  <SolidColorBrush Color="{Binding Cols, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>

Hope this will help.

Upvotes: 2

Related Questions