Cannot set margin to nested grid - wpf

I'm using the Popupbox provided by MaterialDesignInXaml, and I'm trying to assign to nested grid a margin of 8px, so I did:
<GroupBox>
<GroupBox.Header>
<UniformGrid Columns="2">
<TextBlock Text="testo" HorizontalAlignment="Left" />
<materialDesign:PopupBox StaysOpen="True" Grid.Column="1"
Style="{StaticResource PopupBoxStyle}">
<Grid Width="300">
...
</Grid>
</materialDesign:PopupBox
<Style x:Key="PopupBoxStyle" TargetType="{x:Type materialDesign:PopupBox}">
<Style.Resources>
<Style TargetType="{x:Type Grid}">
<Setter Property="Margin" Value="8" />
</Style>
</Style.Resources>
</Style>
the problem's that the header of my GroupBox will increase the height, but if I set the margin without using the PopupBoxStyle the header height of the GroupBox will remain the same.
How can I prevent to enalrge the height of groupbox? What mistake I did creating the PopupBoxStyle? Thanks

How can I prevent to enalrge the height of groupbox?
Set the Margin of the Grid in the ToogleContent to 0 to override the style setter or remove the setter from the style:
<materialDesign:PopupBox.ToggleContent>
<Grid Width = "300" Margin="0">
...
</Grid>
</materialDesign:PopupBox.ToggleContent>

Related

How to make WPF ComboBox items background Visible?

<Grid Grid.Row="5" Grid.Column="1">
<ComboBox Cursor="Hand" SelectedItem="{Binding SelectedRealEstate}" Background="White"
Name="cbbRealEstates" ItemsSource="{Binding RealEstateSummary}"/>
</Grid>
the code above gives me invisible item background
How do I make the background visible?
According to this,
You will have to set the styles in the resources of the element. In my case it's a window. So it's
<Window.Resources>
<Style x:Key="ComboBoxItemStyle" TargetType="ComboBoxItem">
<Setter Property="Background" Value="Blue" />
</Style>
<Style x:Key="ComboBoxStyle" TargetType="ComboBox">
<Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyle}" />
</Style>
</Window.Resources>
Set a style for ComboBoxItem. And use that style when you set the style for the ComboBox
Then apply the Combobox Style to the element.
<ComboBox Name="myCmb" Style="{StaticResource ComboBoxStyle}">

WPF TextBox BorderBrush Style vs. No Style

Why does a border appear around a TextBox when a Style is used as opposed to no Style being used? Is there a way to set this using a Style so no border appears, like in the "Phone" Grid/TextBox? See below:
WINDOW XAML
<!-- City, State, Zip -->
<Grid Grid.Column="0" Grid.Row="2" Background="White" Margin="50,2,25,2">
<Viewbox>
<TextBox Style="{Binding StandardTextBox1}" Text="{Binding CityStateZip}"/>
</Viewbox>
</Grid>
<!-- Phone -->
<Grid Grid.Column="0" Grid.Row="3" Background="White" Margin="50,2,25,2">
<Viewbox>
<TextBox
Background="Transparent"
BorderBrush="Transparent"
Text="{Binding FacilityPhoneMain}"
TextAlignment="Center"
FontSize="14"
/>
</Viewbox>
</Grid>
STYLE XAML
<!-- Standard TextBox 1 -->
<Style TargetType="TextBox" x:Key="StandardTextBox1">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="FontSize" Value="14"/>
</Style>
You can modify the BorderThickness property of the TextBox in your Style to remove the border.
As for why the border is not affected in the Style, I have a hunch that this is due to dependency property precedence.
Animations have a higher precedence than property setters. Due to the default WPF Textbox template having animations that affect the BorderBrush property, the setters are not being applied.
This link has some relevant information
To quote: "Animation values always take precedence over local values, setters and triggers"

Monospace Margin between elements in WPF

I want the space between the child elements in, for example, StackPanel be the same. When using the same Margin for child elements, gap between neighbors doubles. I'm using a little trick to solve this, but it seems to me there is more elegant solution. May be you have one?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Margin" Value="4,4,0,4" />
...
</Style>
<Style x:Key="LastMyButtonStyle" TargetType="Button" BasedOn="{StaticResource MyButton}">
<Setter Property="Margin" Value="4" />
</Style>
I'm using MyButtonStyle for all buttons except the last one, which use LastMyButtonStyle.
Put the StackPanel in another container, i.e. a Border, and set its Margin to the same value as those of the Buttons:
<Border>
<StackPanel Orientation="Horizontal" Margin="2">
<Button Margin="2" Content="Button 1"/>
<Button Margin="2" Content="Button 2"/>
<Button Margin="2" Content="Button 3"/>
</StackPanel>
</Border>

UserControl in DataTemplate doesn't applied style for font

I have a user control in a DataTemplate, The Style of a TextBlock doesn't change the FontSize but changes the Background.
Attached are the samples:
Create a WPF window.
Create a User control, UserControl1
Inside the Window paste the below code:
<Window.Resources>
<Style TargetType="{x:Type TextBlock}"
x:Key="TextBlockStyleFontAndBackgound">
<Setter Property="FontSize"
Value="20" />
<Setter Property="Background"
Value="Blue" />
</Style>
<DataTemplate x:Key="contentTemplate">
<StackPanel>
<m:UserControl1 />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl FontSize="10">
<StackPanel x:Name="stackPanel">
<Button Click="Button_Click" />
<ContentControl ContentTemplate="{StaticResource contentTemplate}" />
<!--<m:UserControl1 />-->
</StackPanel>
</ContentControl>
</Grid>
In the user control paste the following code:
<UserControl.Resources>
<DataTemplate x:Key="contentTemplateInsideUserControl">
<TextBlock Name="textBlockInResourse" Text="textBlockInsideUserControlResource"
Style="{DynamicResource TextBlockStyleFontAndBackgound}"/>
</DataTemplate>
</UserControl.Resources>
<Grid>
<StackPanel>
<ContentControl ContentTemplate="{StaticResource contentTemplateInsideUserControl}" />
<Button Content="St" Click="Button_Click" />
<TextBlock Name="textBlockInControl" Text="textBlockInsideUserControl"
Style="{DynamicResource TextBlockStyleFontAndBackgound}" />
</StackPanel>
</Grid>
We have 2 text blocks with the same background color, blue, but with different font sizes.
textBlockInResourse FontSize = 20, taken from the style TextBlockStyleFontAndBackgound
textBlockInControl FontSize = 10, inherited value, why does it happen?
I have added a handle in the user control:
private void Button_Click(object sender, RoutedEventArgs e)
{
Style style = FindResource("TextBlockStyleFontAndBackgound") as Style;
textBlockInControl.Style = null;
textBlockInControl.Style = style;
}
And now the Font is set to the style TextBlockStyleFontAndBackgound, and it's size is 20
Why now the FontSize is taken from the style TextBlockStyleFontAndBackgound.
Thanks,
barak
That's a very peculiar problem you have found there. I'm not sure why the FontSize is not affected when not in a DataTemplate... looking at the two property descriptions and remarks on MSDN, the only difference between them is that TextBlock.FontSize is also an AttachedProperty, but I can't see how that would affect anything.
I can however offer a solution to the problem if you're still interested. Try declaring your Style in your App.xaml file:
<Application.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyleFontAndBackgound">
<Setter Property="FontSize" Value="20" />
<Setter Property="Background" Value="Blue" />
</Style>
</Application.Resources>
Then declare your TextBlock in your UserControl using StaticResource like so:
<TextBlock Text="text" Style="{StaticResource TextBlockStyleFontAndBackgound}" />

s:ScatterView ItemTemplate - how to set height or width?

I have a small problem while working on software for a Surface: I have a binded ScatterView and its items have a DataTemplate. My question is : how do I set the width and height of the ScatterViewItem that it is created from the ItemTemplate?
<s:ScatterView Name="svMain" Loaded="svMain_Loaded" ItemsSource="{Binding BallsCollection}" >
<s:ScatterView.ItemTemplate >
<DataTemplate>
<DockPanel LastChildFill="True" >
<DockPanel.Background>
<ImageBrush ImageSource="image\note.png" Stretch="Fill" />
</DockPanel.Background>
<TextBox Background="Transparent" DockPanel.Dock="Top" Text="{Binding Path=Message}"
IsReadOnly="True" TextWrapping="Wrap"></TextBox>
</DockPanel>
</DataTemplate>
</s:ScatterView.ItemTemplate>
</s:ScatterView>
I believe you can set that through the ItemContainerStyle, like with other ItemsControls, but i'm not certain, as i do not have the Surface SDK.
<s:ScatterView.ItemContainerStyle>
<Style TargetType="{x:Type s:ScatterViewItem}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="100"/>
</Style>
</s:ScatterView.ItemContainerStyle>
You can of course also use bindings instead fixed units.

Resources