0

My question is almost the same as Why doesn't style-setting of the window background work?. The answer to that question is "your style targets Window, but that won't be applied to MainWindow".

In my case, I have the following style which targets RowDefinition. Elsewhere in my xaml, I have a standard Grid with some Row and Column definitions. The target in Application.Resources exactly matches my xaml RowDefinition element, yet the style seems only to apply in the designer, and not at runtime.

I'm running Visual Studio 2012, with Update 4, targeting the .NET Framework 4.5.

App.xaml:

<Application x:Class="MyProj.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Application.Resources>
        <Style TargetType="RowDefinition">
            <Setter Property="Height" Value="35" />
        </Style>
    </Application.Resources>
</Application>

DriverEdit.xaml:

<Window x:Class="MyProj.Windows.DriverEdit"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:p="clr-namespace:MyProj.Properties"
        xmlns:Controls="clr-namespace:MyProj.Controls"
        WindowStartupLocation="CenterOwner"
        Title="{Binding Path=SelectedItem.UniqueName, TargetNullValue='New'}" Height="300" Width="300">
    <DockPanel>
        <StackPanel DockPanel.Dock="Right">
            <Controls:CommonEditButtons />
        </StackPanel>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <TextBlock Grid.Column="0" Grid.Row="0" Text="{x:Static p:Resources.CommonEditID}" />
            <TextBlock Grid.Column="0" Grid.Row="1" Text="{x:Static p:Resources.CommonEditName}" />

            <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding Path=SelectedItem.Id}" />
            <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=SelectedItem.Name}" />
        </Grid>
    </DockPanel>
</Window>

Edited to provide more XAML.

Community
  • 1
  • 1
epalm
  • 4,283
  • 4
  • 43
  • 65
  • I've created a WPF project and tried to recreate your steps. The RowDefiniton Style from the Resources is applied as it should (the rows have the given height). Could it be that something at runtime is overriding the style, maybe a trigger? How are you judging the height at runtime? Try filling the rows with colored rectangles to get a good visual feedback. – ChrisK Nov 15 '13 at 21:59
  • The window containing the `Grid` has no defined resources, and `Application.Resources` contains only that style. The grid contains TextBlocks and TextBoxes, and the rows are stretched to the height of the `Grid` at runtime. – epalm Nov 15 '13 at 22:07
  • Does putting the Grid inside a StackPanel give you the results you want? – Crispy Nov 15 '13 at 22:25
  • No, if I set the RowDefinition Height property to 35 in Application.Resources and wrap the Grid in a StackPanel, then at runtime this simply squashes the Grid into the smallest space it can given the items it contains (each row is less than 35), while in the designer it appears correctly with a row height of 35. – epalm Nov 16 '13 at 00:42
  • To clarify, at runtime, the Grid on its own has row heights to large (occupies max available space), and the Grid wrapped in a StackPanel has row heights too small (occupies min available space). In both cases, the row height specified in Application.Resources is observed in the designer, but ignored at runtime. – epalm Nov 16 '13 at 00:46
  • Could you maybe post a more complete XAML (if the application isn't to complex)? – ChrisK Nov 16 '13 at 14:12
  • @ChrisK, Edited to provide more XAML. – epalm Nov 20 '13 at 19:03
  • The ``-Part is located where exactly? – ChrisK Nov 20 '13 at 19:26
  • Edited again. The Application element is located in App.xaml. – epalm Nov 20 '13 at 19:39

0 Answers0