I am using DynamicResources in WPF, I have made a button style in app.xaml. Below is the definition:
<Application.Resources>
<Style x:Key="InteractionButtonStyle"
TargetType="{x:Type Button}">
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
<SolidColorBrush x:Key="StyleBasedForegroundColor" Color="White"/>
<SolidColorBrush x:Key="StyleBasedBackgroundColor" Color="Black" />
</Style.Resources>
<Setter Property="Background" Value="{StaticResource StyleBasedBackgroundColor}" />
<Setter Property="Foreground" Value="{StaticResource StyleBasedForegroundColor}" />
<Setter Property="FontSize" Value="15" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="10" />
</Style>
</Application.Resources>
Now when I use this style in a page:
<Button Grid.Row="3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Click="RegisterNew_Clicked"
Style="{StaticResource InteractionButtonStyle}"
Visibility="{Binding ShowRegisterVeteranOption, Converter={StaticResource BoolToVisibilityConverter}}">
<StackPanel Orientation="Horizontal">
<Viewbox Height="20"
Width="20">
<Path Data="M9.6110058,5.399L11.650064,5.399 11.650064,9.6789093 15.929999,9.6789093 15.929999,11.717999 11.650064,11.717999 11.650064,15.998 9.6110058,15.998 9.6110058,11.717999 5.3309995,11.717999 5.3309995,9.6789093 9.6110058,9.6789093z M10.66645,1.6420071C5.6808301,1.6420071 1.6404767,5.6823504 1.6404767,10.66655 1.6404767,15.650881 5.6808301,19.691223 10.66645,19.691223 15.64948,19.691223 19.689723,15.650881 19.689723,10.66655 19.689723,5.6823504 15.64948,1.6420071 10.66645,1.6420071z M10.66645,0C16.546567,0 21.333001,4.785153 21.333001,10.66655 21.333001,16.548068 16.546567,21.333 10.66645,21.333 4.7836227,21.333 0,16.548068 0,10.66655 0,4.785153 4.7836227,0 10.66645,0z"
Fill="{DynamicResource StyleBasedForegroundColor}" />
</Viewbox>
<TextBlock Text="Register"
Margin="10,0,0,0"
Foreground="{DynamicResource StyleBasedForegroundColor}"
VerticalAlignment="Center" />
</StackPanel>
</Button>
Although, things work when the app is running, I get this annoying warning:
The resource "StyleBasedForegroundColor" could not be resolved.
I looked up other questions, but could not find a way that doesn't involve themes.xaml or ResourceDictionary
I am trying to achieve my save button to reusable button and original button style is like this=>
<Button Margin="5"
Padding="0"
Width="98"
Cursor="Hand"
x:Name="btnSave"
Click="btnSave_Click">
<StackPanel Orientation="Horizontal" Height="25" Width="90">
<Image Source="\Image\Other\Save.ico" Width="20" Margin="3 0"></Image>
<TextBlock VerticalAlignment="Center" Margin="15 0">Save</TextBlock>
</StackPanel>
</Button>
Just text and image. So I just want to use this button as a reusable button. So I move this button to App.xaml Like this=>
<Style TargetType="Button" x:Key="SaveButton" BasedOn="{StaticResource MetroButton}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Width" Value="98"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="12"/>
<Style.Resources>
<Style TargetType="StackPanel">
<Setter Property="Orientation" Value="Horizontal"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="90"/>
<Setter Property="Background" Value="Red"/>
<Style.Resources>
<Style TargetType="Image">
<Setter Property="Source" Value="/Image/Other/Save.ico"/>
<Setter Property="Width" Value="20"/>
<Setter Property="Margin" Value="3 0"/>
</Style>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="15 0"/>
<Setter Property="Text" Value="Save"/>
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
But after moving that, This button is not working anymore. Please let me know why this one is not working.
You said you wanted to reuse styles, so you shouldn't nest them. The right thing to do is:
<Style x:Key="SaveButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel Orientation="Horizontal" Height="25" Width="90" Background="Red">
<Image/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You need to define the StackPanel somewhere and set the Button's Content property to it. You may define the StackPanel as a non-shared resource next to the Style in App.xaml like this:
<StackPanel x:Key="sp" x:Shared="False" Orientation="Horizontal" Height="25" Width="90">
<Image Source="screen.png" Width="20" Margin="3 0"></Image>
<TextBlock VerticalAlignment="Center" Margin="15 0">Save</TextBlock>
</StackPanel>
<Style TargetType="Button" x:Key="SaveButton">
<Style.Resources>
</Style.Resources>
<Setter Property="Margin" Value="5"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Width" Value="98"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Content" Value="{StaticResource sp}" />
</Style>
I am using a WPF toolkit chart in my application which is for touch screen devices. I want to enable scrolling for the chart but don't want to show scroll bar to the user. How can I achieve this? Here is my code:-
<ScrollViewer>
<chartingToolkit:Chart Name="bottomChart" BorderThickness="0" Padding="0" ClipToBounds="False" >
<chartingToolkit:Chart.PlotAreaStyle>
<Style TargetType="Grid">
<Setter Property="Background" Value="Transparent" />
</Style>
</chartingToolkit:Chart.PlotAreaStyle>
<chartingToolkit:Chart.Axes>
<chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="False" Visibility="Hidden" Width="0" Minimum="0"/>
<chartingToolkit:LinearAxis Orientation="X" Interval="5" ShowGridLines="True" Visibility="Hidden" Height="0" >
</chartingToolkit:LinearAxis>
</chartingToolkit:Chart.Axes>
<chartingToolkit:Chart.TitleStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0" />
<Setter Property="Height" Value="0" />
<Setter Property="Visibility" Value="Hidden" />
</Style>
</chartingToolkit:Chart.TitleStyle>
<chartingToolkit:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0" />
<Setter Property="Height" Value="0" />
<Setter Property="Visibility" Value="Hidden" />
</Style>
</chartingToolkit:Chart.LegendStyle>
<TestManagementUC:UnorderedLineSeries x:Name="BottomChartSeries" DependentValueBinding="{Binding Path=Value}" IndependentValueBinding="{Binding Path=Key}" ItemsSource="{Binding Path=DataPoints}">
<TestManagementUC:UnorderedLineSeries.DataPointStyle>
<Style TargetType="{x:Type chartingToolkit:LineDataPoint}">
<Setter Property="Background" Value="{StaticResource Brush}" />
<Setter Property="Template" Value="{x:Null}" />
</Style>
</TestManagementUC:UnorderedLineSeries.DataPointStyle>
</TestManagementUC:UnorderedLineSeries>
</chartingToolkit:Chart>
</ScrollViewer>
I have the style:
<Style TargetType="Panel" x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type styles:CommonStyles}, ResourceId=ButtonsPanelStyle}">
<Style.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
<Setter Property="MinWidth" Value="75"/>
</Style>
</Style.Resources>
<Setter Property="Background" Value="#E0E0E0"/>
<Setter Property="StackPanel.Orientation" Value="Horizontal"/>
<Setter Property="FlowDirection" Value="RightToLeft"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="MinWidth" Value="{Binding RelativeSource={RelativeSource Self}, Path=DesiredSize.Width}"/>
</Style>
And its usage:
<DockPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"
Style="{DynamicResource {x:Static styles:CommonStyles.ButtonsPanelStyleKey}}">
<Button Content="Add" Command="{Binding Add}" Grid.RowSpan="2" Height="23" HorizontalAlignment="Right" Name="AddButton" VerticalAlignment="Center" TabIndex="2" >
<Button.Style>
<Style BasedOn="{StaticResource ResourceKey={x:Type Button}}" TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding ActionType}" Value="Edit">
<Setter Property="Button.Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</DockPanel>
The style applied to button does not respect margin set in the style defined inside style's resources. Why?
WPF ListBoxItem how to wrap text in it? My Item container style looks like this:
<Style x:Key="GroupListBoxItemStyle"
TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="FocusVisualStyle"
Value="{x:Null}" />
<Setter Property="FontSize"
Value="11" />
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Width"
Value="95" />
<Setter Property="HorizontalAlignment"
Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<SlidingBar:SlidingBarRadioButton GroupName="PermissionsRadioButtonGroup"
IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},BindsDirectlyToSource=True,Mode=TwoWay}"
Text="{Binding Converter={StaticResource resourceStringToResourceConverter}}"
ImageSource="{Binding Converter={StaticResource PermissionTypeToImageConverter}}"
Margin="1"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Make sure you set the following properties to the ListBox/ListView:
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"