0

Im trying to update a style of a button, in cs file (c#), that I created in xaml code.

I searched for a lot solutions but none worked.

<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)
        {

        }

I have this
regular icon
I want this
highlighted icon
Ignore the difference between the two icons

Shashank Shekhar
  • 3,958
  • 2
  • 40
  • 52
LOL Jovem
  • 197
  • 2
  • 17
  • Possible duplicate of [How to set the style programmatically](https://stackoverflow.com/questions/3199424/how-to-set-the-style-programmatically) – Larry Tang Apr 29 '19 at 15:28
  • @LarryTang I saw that solution but it's so confusing, may have a better and not that confused solution, and thats what im searching for. And I want update not create in my c# file. – LOL Jovem Apr 29 '19 at 15:31
  • Is there a reason you don't want the style declared/used in your `Xaml`? – TaylorD Apr 29 '19 at 18:13
  • @TaylorD I want my style declared in my Xaml file because the FlowListView code, but i can delcare some style in a ResourceDictionary if it is better. I just want to be able to change my button style through cs file(c#). (Sry for the delay) – LOL Jovem Apr 30 '19 at 08:08

1 Answers1

0

According to your description, you create style for Button, then you want to change some Button property when Button click, I suggest you can change Button property directly, like this:

XAML

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

CS

private void CategoryButtons_Clicked(object sender, EventArgs e)
{
    Button btn = (Button)sender;       
    btn.BorderColor = Color.Orange;
   }
Cfun
  • 8,442
  • 4
  • 30
  • 62
Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16
  • Thank you very much, this solution worked, I did a little bit different, didnt create the ResourceDictionary and the other button, I just kept with my Xaml code and did the c# code that you sent me. But now, if you could help me, when I click again in the button can i reset the style? Like, I click one time in the button the style changes, but when I click again I want to reset the styles. – LOL Jovem Apr 30 '19 at 08:43