WPF: TextBox materialDesign - how to Alignment HintAssist.Hint - wpf

So I have this TexBox:
<TextBox Height="30"
BorderThickness="0"
materialDesign:TextFieldAssist.HasFilledTextField="False"
materialDesign:TextFieldAssist.HasOutlinedTextField="False"
materialDesign:TextFieldAssist.DecorationVisibility="Hidden"
materialDesign:TextFieldAssist.UnderlineBrush="Transparent"
materialDesign:TextFieldAssist.TextBoxViewMargin="10,3,0,0"
materialDesign:TextFieldAssist.HasClearButton="True"
materialDesign:HintAssist.Hint="Search"
materialDesign:HintAssist.HelperTextFontSize="12"
materialDesign:HintAssist.IsFloating="False"/>
And I want to place my Hint (Search in my example) at the center of my TexBox.
Al i can found is to change TextFieldAssist.TextBoxViewMargin but of course this effect the input text as well.

Just add this code to textbox:
<TextBox.Resources>
<Style TargetType="{x:Type materialDesign:SmartHint}">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</TextBox.Resources>
Example
<TextBox Height="30"
BorderThickness="0"
materialDesign:TextFieldAssist.HasFilledTextField="False"
materialDesign:TextFieldAssist.HasOutlinedTextField="False"
materialDesign:TextFieldAssist.DecorationVisibility="Hidden"
materialDesign:TextFieldAssist.UnderlineBrush="Transparent"
materialDesign:TextFieldAssist.TextBoxViewMargin="10,3,0,0"
materialDesign:TextFieldAssist.HasClearButton="True"
materialDesign:HintAssist.Hint="Search"
materialDesign:HintAssist.HelperTextFontSize="12"
materialDesign:HintAssist.IsFloating="False">
<TextBox.Resources>
<Style TargetType="{x:Type materialDesign:SmartHint}">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</TextBox.Resources>
</TextBox>

Related

DatePicker do not show full month name at top because of style inheritance

I have a situation where i have to display the DatePicker , The problem is i am not able to see the complete month name at top, i am just able to see the first digit of december
with dots after like "D.." whereas there is enough space to show entire month name. Its because it is inheriting many style of textbox/textblock from controltemplate which i am not including.
Here is my try :
<DatePicker
FontSize="6"
SelectedDate="{x:Static sys:DateTime.Now}"
HorizontalContentAlignment="Left">
</DatePicker>
<Style TargetType="{x:Type DatePickerTextBox}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox
x:Name="PART_TextBox"
HorizontalAlignment="Left"
VerticalContentAlignment="Center"
Text="{Binding Path=SelectedDate, ConverterCulture={x:Static glob:CultureInfo.InvariantCulture}, StringFormat={}{0:yyyy-MM-dd}, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsReadOnly" Value="True" />
</Style>
<Style TargetType="{x:Type DatePicker}">
<Setter Property="Background" Value="{StaticResource FlowWhiteBrush}" />
</Style>
How to fix it such that we override the previous style by rewriting in the template written above and make it's alignment in left so that all month appears? But which styles and properties to be overridden?

WPF: cannot change style SolidColorBrush via code behind because read-only state [duplicate]

This question already has an answer here:
Binding SolidColorBrush to ControlTemplate Background
(1 answer)
Closed 5 years ago.
So inside Window.Resources i have this SolidColorBrush:
And in separate file (GridViewColumnHeader.xaml) i have this Style:
<Style x:Key="ListViewHeaderDefaultStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<Border BorderThickness="0,0,0,1" BorderBrush="Gray" Background="Transparent">
<TextBlock
x:Name="ContentHeader"
Text="{TemplateBinding Content}"
Padding="0,5,0,0"
Width="{TemplateBinding Width}"
TextAlignment="Left"
Margin="5,0,0,0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Foreground" Value="{DynamicResource GridViewColumnHeaderForegroundColor}" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="12" />
</Style>
Now want i try to do is change my Style Foreground color via code behind:
SolidColorBrush solidColorBrush = (SolidColorBrush)this.TryFindResource("GridViewColumnHeaderForegroundColor");
if (solidColorBrush != null)
solidColorBrush.Color = Colors.Black;
But from some reason got this InvalidOperationException:
Additional information: Cannot set a property on object '#FFDCDCDC'
because it is in a read-only state.
Instead of changing your Resource Brush, you could define another brush and assign it to the foreground of your style.
However, there is another way of accomplishing your goal.
Have a look here.
First of all you should declare the used style as dynamicresource aswell.
Example:
<ListView VerticalAlignment="Bottom" Height="63" IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView x:Name="test" ColumnHeaderContainerStyle="{DynamicResource ListViewHeaderDefaultStyle}" >
<GridViewColumn Header="header1"/>
</GridView>
</ListView.View>
</ListView>
Now you can simply change the Foreground Color by changing the resource brush like that:
this.Resources["GridViewColumnHeaderForegroundColor"] = Brushes.Black;

Making all images be of the same size and margin

I noticed that I've got a few images in a stack panel and every such has exactly the same size, margin, alignment etc. set to it. I don't want to create a global resource for style. Is it possible to have a local style declared in this specific panel and only for the images (TargetType is enough in this case)?
I'd like something along this solution with the exception that I don't use the global style resource.
<StackPanel.Resources>
<Style TargetType="Image">
<Setter Property="Width" Value="24" />
...
</Style>
</StackPanel.Resources>
<Image Source="{StaticResource Poof}"
VerticalAlignment="Top"
...
Margin="20,20,20,0" />
Just put the style in the resources for the panel, and it will apply to all items of that type, inside the panel.
For example:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Width" Value="200" />
</Style>
</StackPanel.Resources>
<TextBox Margin="20" />
<TextBox Margin="20" />
<TextBox Margin="20" />
<TextBox Margin="20" />
</StackPanel>
Results in all the text boxes having a width of 200 units.
Unless, you override the style with a setting on the individual items. Perhaps you are doing that?
Try add style inside resources.
<Page.Resources>
<Style TargetType="Image">
<Setter Property="Width" Value="80"/>
<Setter Property="Height" Value="80"/>
</Style>
</Page.Resources>

How to write a style for HyperlinkButton Shadow effect in Silverlight

I have a styled HyperlinkButton as per custom need as follow:
<HyperlinkButton Tag="Transactions/HomeWorkPage"
Background="#E9E9EB" Foreground="Black">
<HyperlinkButton.Effect>
<DropShadowEffect Direction="270" Opacity="0.35" ShadowDepth="3"/>
</HyperlinkButton.Effect>
<StackPanel Orientation="Horizontal">
<Image Source="/SchoolMgmt;component/Assets/Images/R_Homework.png"
Margin="10,0,1,0"/>
<telerik:Label Content="Home Work" CharacterSpacing="25"
telerik:StyleManager.Theme="{Binding SelectedTheme,Mode=TwoWay}"
FontSize="12" FontFamily="Arial Rounded MT" Margin="3"/>
</StackPanel>
</HyperlinkButton>
Now with satisfy with the result I started to write a style resource for this so that I can use wherever I want.
This is I am able to write:
<Style x:Name="QuickLinkStyle" TargetType="HyperlinkButton">
<Setter Property="Background" Value="#E9E9EB"/>
<Setter Property="Foreground" Value="Black"/>
</Style>
I am not able to convert following into style
<HyperlinkButton.Effect>
<DropShadowEffect Direction="270" Opacity="0.35" ShadowDepth="3"/>
</HyperlinkButton.Effect>
I am not a designer and not using expression blend for writing a style. I tried to googled it. But all I able to find out is DataTemplate , VisualStates etc. Its very confusing.
So this is not working?
<Style x:Name="QuickLinkStyle" TargetType="HyperlinkButton">
...
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect .../>
</Setter.Value>
</Setter>
</Style>

Custom AppBarButton from image

Step by step, I'm optimizing my xaml to create a custom AppBarButton from an image. I've gone from a complete custom xaml layout to using a few lines using styles, but I know I can simplify this one more step.
Here's what I currently have:
<Style x:Key="PrintAppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="PrintAppBarButton"/>
<Setter Property="AutomationProperties.Name" Value="Print"/>
</Style>
<Button Style="{StaticResource PrintAppBarButtonStyle}">
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Button>
I know I can move the image source into the style definition, but I haven't been able to get this to work. After reading about the AppBarButton class, I tried to set my TargetType to AppBarButton and then set an Icon property, but was unsuccessful. Something like the following:
<Style x:Key="PrintAppBarButtonStyle" TargetType="AppBarButton" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="PrintAppBarButton"/>
<Setter Property="AutomationProperties.Name" Value="Print"/>
<Setter Property="Icon">
<Setter.Value>
// here it's expecting an IconElement
</Setter.Value>
</Setter>
</Style>
<Button Style="{StaticResource PrintAppBarButtonStyle}"/>
Any tips?
The Icon property does not accept an image -- if you need to use an image, stick with the Content property. That can also be styled:
<Style x:Key="PrintAppBarButtonStyle" TargetType="ButtonBase" BasedOn="{StaticResource AppBarButtonStyle}">
<Setter Property="AutomationProperties.AutomationId" Value="PrintAppBarButton"/>
<Setter Property="AutomationProperties.Name" Value="Print"/>
<Setter Property="Content">
<Setter.Value>
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Setter.Value>
</Setter>
</Style>
By way of explanation, note that "ContentControl.Content" is the ContentProperty, which means that the child element gets set as "Content". Ie, this:
<Button Style="{StaticResource PrintAppBarButtonStyle}">
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Button>
is just shorthand for this:
<Button Style="{StaticResource PrintAppBarButtonStyle}">
<Button.Content>
<ContentControl>
<Image Source="/Assets/AppBar/appbar_printer_dark.png"/>
</ContentControl>
</Button.Content>
</Button>

Resources