Как я могу обновить стиль кнопки в файле cs? - PullRequest
0 голосов
/ 29 апреля 2019

Я пытаюсь обновить стиль кнопки в файле cs (c #), который я создал в коде xaml.

Я искал много решений, но ни одно не помогло.

<flv:FlowListView FlowColumnCount="3" SeparatorVisibility="None" HasUnevenRows="true"
                    FlowItemTappedCommand="{Binding ItemTappedCommand}" FlowLastTappedItem="{Binding LastTappedItem}"
                    FlowItemsSource="{Binding MyCategories}" >

                    <flv:FlowListView.FlowColumnTemplate>
                        <DataTemplate>
                            <Button Text="{Binding Name}"
                                TextColor="White"
                                x:Name="CategoryButtons"
                                Clicked="ButtonSelected"
                                ContentLayout="Top"
                                BackgroundColor="Transparent"
                                BorderColor="White"
                                BorderWidth="2"
                                CornerRadius="6"
                                Margin="5,5,5,10" />
                        </DataTemplate>
                    </flv:FlowListView.FlowColumnTemplate>

                </flv:FlowListView>
 public void ButtonSelected(object sender, EventArgs e)
        {

        }

У меня есть
regular icon
Я хочу это
highlighted icon
Не обращайте внимания на разницу между двумя значками

1 Ответ

0 голосов
/ 30 апреля 2019

Согласно вашему описанию, вы создаете стиль для Button, затем вы хотите изменить какое-либо свойство Button при нажатии кнопки, я предлагаю вам изменить свойство Button напрямую, например так:

 <StackLayout>
    <Label
        HorizontalOptions="Center"
        Text="Welcome to Xamarin.Forms!"
        VerticalOptions="CenterAndExpand" />

    <flv:FlowListView
        FlowColumnCount="3"
        FlowItemsSource="{Binding categories}"
        HasUnevenRows="True"
        SeparatorVisibility="None">
        <flv:FlowListView.FlowColumnTemplate>
            <DataTemplate>
                <Button
                    x:Name="CategoryButtons"
                    Margin="5,5,5,10"
                    Clicked="CategoryButtons_Clicked"
                    Style="{DynamicResource buttonstyle}"
                    Text="{Binding Name}" />
            </DataTemplate>
        </flv:FlowListView.FlowColumnTemplate>
    </flv:FlowListView>

    <Button
        x:Name="btn1"
        BackgroundColor="Transparent"
        BorderColor="White"
        BorderWidth="2"
        Clicked="btn1_Clicked"
        HeightRequest="40"
        Text="this is test!"
        WidthRequest="300" />
</StackLayout>

  <Application.Resources>
    <ResourceDictionary>
        <Style x:Key="buttonstyle" TargetType="Button">
            <Setter Property="BackgroundColor" Value="Transparent" />
            <Setter Property="BorderWidth" Value="2" />
            <Setter Property="CornerRadius" Value="6" />
            <Setter Property="ContentLayout" Value="Top" />
            <Setter Property="TextColor" Value="White" />
            <Setter Property="BorderColor" Value="White" />

        </Style>
    </ResourceDictionary>
</Application.Resources>


  private void CategoryButtons_Clicked(object sender, EventArgs e)
    {
        Button btn = (Button)sender;

        btn.BorderColor = Color.Orange;



    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...