locked
Style via idiom not working RRS feed

  • Question

  • User69073 posted

    Background: wanting to set a grid's rowdefinition and columndefinition height and width respectively via a style, with the setter using an idiom. Idea being grid rows and columns would be smaller on a phone than tablet. This is in a netstandard 2.0 project.

    Compiles and runs fine, but has no effect:

    ```xml

    ```

    Wondering if this is a legit bug with netstandard 2.0.

    Friday, August 10, 2018 2:41 PM

Answers

  • User69073 posted

    Here's my workaround:

    ```xml

    ...

                   <Grid x:Name="LayoutRoot" RowSpacing="5" ColumnSpacing="5" BackgroundColor="Gray">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="{StaticResource GridSize}" />
                            <ColumnDefinition Width="{StaticResource GridSize}" />
                            <ColumnDefinition Width="{StaticResource GridSize}" />
                        </Grid.ColumnDefinitions>
                    </Grid>
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, August 10, 2018 4:59 PM

All replies

  • User56293 posted

    I'm wondering if this doesn't work because the RowDefinition/ColumnDefinition types doesn't derive from VisualElement, which has the Style property. I presume you can only set an implicit style to a type that has the Style property defined.

    Friday, August 10, 2018 2:51 PM
  • User180523 posted

    Hmmm... Interesting thought. In the mean time though... Can you get the same affect (at least for height) by not specifying the RowHeight at all but adjusting the font size by idiom? Smaller font on phone should automatically mean a smaller height if the row is self-sizing to content. Right?

    Or if the the rows and columns are not set to fixed sizes but instead 1* and 5* etc., wouldn't they automatically adjust to the real estate of the device and orientation automatically?

    Friday, August 10, 2018 2:51 PM
  • User69073 posted

    Yes this works fine:

    xml <Setter Property="FontSize"> <Setter.Value> <OnIdiom x:TypeArguments="x:Double" Tablet="75" Phone="35" /> </Setter.Value> </Setter>

    Friday, August 10, 2018 3:00 PM
  • User69073 posted

    @PaulDiston said: I'm wondering if this doesn't work because the RowDefinition/ColumnDefinition types doesn't derive from VisualElement, which has the Style property. I presume you can only set an implicit style to a type that has the Style property defined.

    Makes sense. I supposed I can add a property to my viewmodel and check the idiom in my codebehind, but thats sort of a kludge.

    Friday, August 10, 2018 3:07 PM
  • User56293 posted

    I would probably use a converter, as checking the Idiom in the ViewModel isn't nice.

    Friday, August 10, 2018 3:09 PM
  • User180523 posted

    A property for what? What would you have to do in code-behind (yikes)? Set the font size in the style. Set the columnwidth to dynamic/relative sizes. That's all XAML.

    Friday, August 10, 2018 3:09 PM
  • User180523 posted

    @PaulDiston said: I would probably use a converter, as checking the Idiom in the ViewModel isn't nice.

    More importantly... Using a ViewModel for display needs is just plain wrong from an MVVM perspective. A ViewModel doesn't know or care about the 0:n views that could be using it as BindingContext. You could have 20 different views all looking at the same ViewModel. You don't want 20 properties for something in that case.

    That's why the View itself should be taking care of this.

    I don't don't see what you need code or a converter for. What is it you feel you need to calculate?

    Friday, August 10, 2018 3:25 PM
  • User69073 posted

    Here's my workaround:

    ```xml

    ...

                   <Grid x:Name="LayoutRoot" RowSpacing="5" ColumnSpacing="5" BackgroundColor="Gray">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                            <RowDefinition Height="{StaticResource GridSize}" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="{StaticResource GridSize}" />
                            <ColumnDefinition Width="{StaticResource GridSize}" />
                            <ColumnDefinition Width="{StaticResource GridSize}" />
                        </Grid.ColumnDefinitions>
                    </Grid>
    

    ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, August 10, 2018 4:59 PM