0

これは、ボタンが楕円のように見えるスタイルを作成する XAML です。また、キーを使用して色を作成したので、変更したり、バインドしたりできます(運が悪いとは限りません)。バインディングの問題は、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>

私はC#でこれをやろうとしましたPlayer1 = new Style(typeof(Button)); Player1.Setters.Add(new Setter(Ellipse.FillProperty, new SolidColorBrush(GameInstance.Color1)));

しかし、私はちょうどボタンを取得します

次に、ControlTemplate、グリッド、楕円を作成し、XAML を正確にコピーするために楕円を塗りつぶしました。

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

しかし、私がやろうとするとエラーが発生しますPlayer1.Setters.Add(new Setter(ct1 etc etc)

XAML を使用したバインディングまたは C シャープでそれを行う方法のいずれかで、どんな助けも大歓迎です。独自のクラスのボタンを作成しようとしましたが、再び問題が発生しました。

4

2 に答える 2

2

これを試して

<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;
    }

これは、c# で実行できる多くの方法の 1 つです。ただし、Trigger を使用して実行することもできます。バインディングの使用 この方法で実行できます。

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

これが役立つことを願っています。

于 2012-02-28T18:40:12.347 に答える
2

楕円の塗りつぶしの色をボタンの背景にバインドできます。したがって、ボタンの背景を変更するだけです。

テンプレート :

<Ellipse Fill="{TemplateBinding Background}"/>
于 2012-02-29T12:34:08.117 に答える