How to set Margin to the StackPanel childrens depends up on the StackPanel Orientation, for example Orientatioin is horizontal then set margin 0,0,10,0 or orientation is Vertical then set margin 0,10,0,0.
I try to find any idea but till now i not find anything, can you help me?
Thanks
Edit:
<StackPanel Orientation="Horizontal">
<Button Content="Q" />
<Button Content="W" />
<Button Content="E" />
<Button Content="R" />
<Button Content="T" />
<Button Content="Y" />
<Button Content="U" />
<Button Content="I" />
</StackPanel>
Try this.
<StackPanel Name="stack" Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="0,0,0,10"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=stack, Path=Orientation}" Value="Horizontal">
<Setter Property="Margin" Value="0,0,10,0" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<Button Content="Q" />
<Button Content="W" />
<Button Content="E" />
<Button Content="R" />
<Button Content="T" />
<Button Content="Y" />
<Button Content="U" />
<Button Content="I" />
</StackPanel>
Related
How can I make Label text Underline in WPF? I am stucked and could not find any property for underline:
<Label Name="lblUserName"
Content="Username"
FontSize="14" FontWeight="Medium" />
In Label no TextDecorations, therefore try this:
<Label Width="100" Height="30">
<TextBlock TextDecorations="Underline">TestText</TextBlock>
</Label>
Edit: more universal solution
In this case, instead of Label.Content using Label.Tag, because Content property can be set only once:
<Label Tag="TestContent"
Width="100"
Height="30"
HorizontalContentAlignment="Center"
Background="AliceBlue">
<TextBlock TextDecorations="Underline"
Text="{Binding Path=Tag,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type Label}}}" />
</Label>
Here's a way to apply the style directly to the Label:
<Style TargetType="Label">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" TextDecorations="Underline"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
This simplifies the label items:
<Label>
Label 1
</Label>
<Label Grid.Row="1">
Label 2
</Label>
This works if the content of the labels text only.
Here's an answer with styles.
Content:
<Label>
<TextBlock Style="{DynamicResource StyleName}">text content</TextBlock>
</Label>
And the style:
<Style x:Key="StyleName">
<Setter Property="TextBlock.TextDecorations" Value="Underline" />
<Setter Property="TextBlock.FontStyle" Value="Italic" />
</Style>
I have the following code for an Expander:
<Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
FontSize="18" FontFamily="Calibri" FontWeight="Bold">
<StackPanel>
<Label Content="{StaticResource companyLinksItemSummary}"
FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
<Label Content="{StaticResource companyLinksItemInfo}"
FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
<Label Content="{StaticResource companyLinksItemIssues}"
FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
<Label Content="{StaticResource companyLinksItemMessages}"
FontSize="14" FontFamily="Calibri" FontWeight="Bold"/>
</StackPanel>
</Expander>
The StaticResources are defined as follows (in my resource dictionary):
<sys:String x:Key="companyLinksHeader">company</sys:String>
<sys:String x:Key="companyLinksItemSummary">summary</sys:String>
<sys:String x:Key="companyLinksItemInfo">info</sys:String>
<sys:String x:Key="companyLinksItemIssues">issues</sys:String>
<sys:String x:Key="companyLinksItemMessages">messages</sys:String>
Is there a way to define a dictionary entry (or something else) that will handle the Font styling for the Header and Labels so that I don't have to define the same font over and over (and only change it in one place should I want to change the font)?
EDIT
I found a solution (thanks to those that posted) and am using the following Style for the StackPanel Label items:
<!-- Expander Items text style -->
<Style x:Key="expanderItemsTextStyle">
<Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
<Setter Property="Label.FontWeight" Value="Normal"></Setter>
<Setter Property="Label.FontSize" Value="14"></Setter>
<Setter Property="Label.Foreground" Value="Aqua"></Setter>
</Style>
and implementing it like this (applying it to the StackPanel so it affects all Labels):
<Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
Style="{StaticResource expanderHeaderTextStyle}">
<StackPanel Style="{StaticResource expanderItemsTextStyle}">
<Label Content="{StaticResource companyLinksItemSummary}"/>
<Label Content="{StaticResource companyLinksItemInfo}" />
<Label Content="{StaticResource companyLinksItemIssues}" />
<Label Content="{StaticResource companyLinksItemMessages}" />
</StackPanel>
</Expander>
One thing that does not work though is the Label.Foreground. The foreground color remains black but I can change the font, size or weight via the style. If I move the style into the Label definition though the color works. Is this a bug or is there a different property that will set the font color (foreground) of the StackPanel Labels.
You can use a Style within Window.Resources, and refer to this style using BasedOn within the StackPanel.Resources section. This will apply the styles to all labels inside that StackPanel.
<Window>
<Window.Resources>
<Style x:Key="myLabelStyle" TargetType="{x:Type Label}">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</Window.Resources>
<Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
FontSize="18" FontFamily="Calibri" FontWeight="Bold">
<StackPanel>
<StackPanel.Resources>
<Style BasedOn="{StaticResource myLabelStyle}" TargetType="{x:Type Label}" />
</StackPanel.Resources>
<Label Content="{StaticResource companyLinksItemSummary}" />
<Label Content="{StaticResource companyLinksItemInfo}" />
<Label Content="{StaticResource companyLinksItemIssues}" />
<Label Content="{StaticResource companyLinksItemMessages}" />
</StackPanel>
</Expander>
</Window>
Use a Style:
<Expander Name="CompanyLinks" Header="{StaticResource companyLinksHeader}"
FontSize="18" FontFamily="Calibri" FontWeight="Bold">
<Expander.Resources>
<Style TargetType="Label">
<Setter Property="FontSize" Value="14" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
</Expander.Resources>
<StackPanel>
<Label Content="{StaticResource companyLinksItemSummary}" />
<Label Content="{StaticResource companyLinksItemInfo}" />
<Label Content="{StaticResource companyLinksItemIssues}" />
<Label Content="{StaticResource companyLinksItemMessages}" />
</StackPanel>
</Expander>
Here the Style targets all Label within Expander.
Declare the Fontsize and Font Name in the Resource file
<FontFamily x:Key="BaseFontFamily">Calibri</FontFamily>
<sys:Double x:Key="BaseFontSize">12</sys:Double>
<Label Content="{StaticResource companyLinksItemMessages}"
FontSize="{StaticResource BaseFontSize}" FontFamily="{StaticResource fntfam}"/>
I have multiple textboxes with TextChanged events bound to the same handler
<TextBox TextChanged="TextBox_TextChanged" />
<TextBox TextChanged="TextBox_TextChanged" />
<TextBox TextChanged="TextBox_TextChanged" />
<TextBox TextChanged="TextBox_TextChanged" />
Is there any way to do something like this?
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBox">
<Setter Property="TextChanged" Value="TextBox_TextChanged" />
</Style>
</StackPanel.Resources>
<TextBox />
<TextBox />
<TextBox />
<TextBox />
</StackPanel>
I know the code above is invalid, but it is something similar to what I want to achieve
If I'm not mistaken, you can put the TextBoxBase.TextChanged="TextBox_TextChanged" attribute on the common container element of all of your text boxes, for ex:
<StackPanel TextBoxBase.TextChanged="TextBox_TextChanged">
<TextBox />
<TextBox />
<TextBox />
<TextBox />
</StackPanel>
If you want it in a style:
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="TextChanged" Handler="TextBox_TextChanged"/>
</Style>
Using Grid to display pictures around edge of screen.
<Image Source="...." Grid.Row="0" Grid.Column="0">
<Image.Tooltip>
<TextBlock>Some narrative..</TextBlock>
<TextBox Name="ToolTipText" Grid.Row="1" Grid.Column="1" />
On MouseOver I want the tooltip to appear in TextBox, but it aways appears centered over the image.
<Style TargetType="ToolTip">
<Setter Property="PlacementTarget"
Value="{Binding ElementName=ToolTipText, Path=Text} />
<Setter Property="Placement" Value="Center" />
Try this
<Grid Name="mainGrid">
<Image Source="...." Grid.Row="0" Grid.Column="0" ToolTipService.PlacementTarget="{Binding ElementName=mainGrid}">
<Image.ToolTip>
<ToolTip Placement="Center">
<TextBlock>Some narrative..</TextBlock>
</ToolTip>
</Image.ToolTip>
</Image>
</Grid>
This
ToolTipService.PlacementTarget="{Binding ElementName=mainGrid}"
Can be replaced by
ToolTipService.PlacementTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"
In #WPF you have ItemTemplateSelectors. But, can you also select an ItemContainerStyle based on the datatype of a bound object?
I am databinding a scatterview. I want to set some properties of the generated ScatterViewItems based on the object in their DataContext. A mechanism similar to ItemTemplateSelector for styles would be great. Is that at all possible? I am now binding to properties in the objects that I am displaying to get the effect, but that feels like overhead and too complex (and most importantly, something that our XU designers can't do by themselves).
This is the XAML that I am using now. Your help is greatly appreciated.
<s:ScatterView x:Name="topicsViewer">
<s:ScatterView.ItemTemplateSelector>
<local:TopicViewerDataTemplateSelector>
<DataTemplate DataType="{x:Type mvc:S7VideoTopic}">
<Grid>
<ContentPresenter Content="{Binding MediaElement}" />
<s:SurfaceButton Visibility="{Binding MailToVisible}" x:Name="mailto" Tag="{Binding Titel}" Click="mailto_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Background="Transparent" Width="62" Height="36">
<Image Source="/Resources/MailTo.png" />
</s:SurfaceButton>
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Height="32">
<s:SurfaceButton Tag="{Binding MediaElement}" x:Name="btnPlay" Click="btnPlay_Click">
<Image Source="/Resources/control_play.png" />
</s:SurfaceButton>
<s:SurfaceButton Tag="{Binding MediaElement}" x:Name="btnPause" Click="btnPause_Click">
<Image Source="/Resources/control_pause.png" />
</s:SurfaceButton>
<s:SurfaceButton Tag="{Binding MediaElement}" x:Name="btnStop" Click="btnStop_Click">
<Image Source="/Resources/control_stop.png" />
</s:SurfaceButton>
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type mvc:S7ImageTopic}">
<Grid>
<ContentPresenter Content="{Binding Resource}" />
<s:SurfaceButton Visibility="{Binding MailToVisible}" x:Name="mailto" Tag="{Binding Titel}" Click="mailto_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Background="Transparent" Width="62" Height="36">
<Image Source="/Resources/MailTo.png" />
</s:SurfaceButton>
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Kassa}">
<ContentPresenter Content="{Binding}" Width="300" Height="355" />
</DataTemplate>
</local:TopicViewerDataTemplateSelector>
</s:ScatterView.ItemTemplateSelector>
<s:ScatterView.ItemContainerStyle>
<Style TargetType="s:ScatterViewItem">
<Setter Property="MinWidth" Value="200" />
<Setter Property="MinHeight" Value="150" />
<Setter Property="MaxWidth" Value="800" />
<Setter Property="MaxHeight" Value="700" />
<Setter Property="Width" Value="{Binding DefaultWidth}" />
<Setter Property="Height" Value="{Binding DefaultHeight}" />
<Setter Property="s:ScatterViewItem.CanMove" Value="{Binding CanMove}" />
<Setter Property="s:ScatterViewItem.CanScale" Value="{Binding CanScale}" />
<Setter Property="s:ScatterViewItem.CanRotate" Value="{Binding CanRotate}" />
<Setter Property="Background" Value="Transparent" />
</Style>
</s:ScatterView.ItemContainerStyle>
</s:ScatterView>
Bart Roozendaal, Sevensteps
How about using a ItemContainerStyleSelector (duh!)... Sorry, it's been a long night