i do have a textbox. When there is error i set borderbrush to new SolidColorBrush(Colors.Red). When error is fixed, i want to switch to default color of border of a textbox. I am doing it in codebehind not xaml.
However it is system dependent. I noticed there is something like
SystemColor.ActiveControl etc. Should i use these and if yes, which one is default border of textbox?
Also i noticed there is something like Textbox.borderbrushproperty.defaultmetadata.defaultvalue, which i did not manage to work.
Any ideas how to switch to default borderbrush? thank you.
Do you have to do it in code-behind? If not, you could use a Trigger which will only change the border color while the trigger condition is met.
<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyUserControl, Path=HasErrors}">
<Setter Property="BorderBrush" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
Why not just save the initial value at startup and use that? You should be able to use the system colors, but if you ever change the default color this will continue to work.
private Color _defBtnColor;
public MyUserControl()
{
_defBtnColor = someButton.Foreground;
}
private void SetBackToDefault()
{
someButton.Foreground = _defBtnColor;
}
Try:
control.BackColor = Color.FromKnownColor(KnownColor.Window);
For more information, see Color.FromKnownColor on MSDN.
Maybe I'm a little late to the party but I think you're asking how to change a color to the system default of control?
someControl.BackColor = SystemColors.Control;
or
sslEditMode.BackColor = SystemColors.ButtonFace;
Related
There's a custom touchscreen keyboard in my app built according to this: https://www.codeproject.com/Articles/32568/A-Touch-Screen-Keyboard-Control-in-WPF.
I also have a ResourceDictionary containing all styles and templates. In the TextBox style, I can set the keyboard ON/OFF:
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="14" />
<Setter Property="Padding" Value="4" />
<Setter Property="k:TouchScreenKeyboard.TouchScreenKeyboard" Value="True"/>
</Style>
I would like the user to be able to turn it on or off from the UI, but can't figure out how to reach this property from code behind. I would like to make it without naming the style, since it's pretty commonly used throughout the app.
I tried this, but (no surprise) get ArgumentNotFoundException:
Style s = Application.Current.FindResource("defTextBox") as Style;
s.RegisterName("Keyboard.TouchScreenKeyboard.TouchScreenKeyBoard",false);
Any help would be appreciated!
You shoud be able to set the TouchScreenKeyboard attached property for an individual TextBox like this:
TouchScreenKeyboard.SetTouchScreenKeyboard(textBox1, false);
Changing the defintion of the implicit Style itself after it has already been applied to all TextBox elements doesn't make much sense though. You should define the default value in XAML and then change the value for individual TextBoxes dynamically at runtime if you need to.
I have created a UserControl that is working just fine. If I set the margin in code all goes well:
<cbtn:BillLister Margin="10,5,10,5" />
However, when i try to set it for everyone of those that are in the wrappannel they are in, this doesn't work:
<WrapPanel.Resources>
<Style TargetType="cbtn:BillLister">
<Style.Setters>
<Setter Property="Margin" Value="150,35,50,35" />
<Setter Property="Background" Value="Black"/>
</Style.Setters>
</Style>
The Background property is working however, so i know that it is targeting the correct UserControls.
I did find this: How to give my custom control an overridable default margin? I tried what he did in there, and still no change.
public partial class BillLister : UserControl
{
public BillLister()
{
InitializeComponent();
Labelcount.Content = "0";
}
static BillLister()
{
MarginProperty.OverrideMetadata(typeof(BillLister), new FrameworkPropertyMetadata(new Thickness(30, 130, 30, 30)));
}
...
Does anyone have any idea what might be going on here? I would hate to have to set the margin on each one manually.
Update to clarify: When trying the second version i don't use the inline margin from the first one. Also, the margin fromthe second example is not applied at all.
I'm trying to change the font color of text in my Windows Phone 8 Application using the following code
RootFrame.Foreground = new SolidColorBrush(Colors.Purple);
This doesn't give me any change in the font color. I'm adding this line in App.xaml.cs constructor.
If I change the background color in the similar way it works. Please Can someone explain me what is the problem with this?
Set name to your textbox using Name attribute in your XAML,
<TextBox Name="textBox1"....>
and change the Foreground color using,
textbox1.Foreground = new SolidColorBrush(Colors.Red);
YourTextBoxName.Foreground = new SolidColorBrush(Colors.Red);
This worked for me in setting the font color of the text throughout the Application instead of setting it individually for every control
(App.Current.Resources["PhoneForegroundBrush"] as SolidColorBrush).Color = Colors.Purple;
Create your Style within the Application Resources, Then You can Add Style using Code.
Create Application Resource :
<Application.Resources>
<Style TargetType="TextBox" x:Key="MyTextBox">
<Setter Property="Width" Value="90" />
<Setter Property="Background" Value="Azure"></Setter>
<Setter Property="Foreground" Value="Purple"></Setter>
</Style>
</Application.Resources>
Then You can style like this.
YourTextBoxName.Style = Application.Current.Resources["MyTextBox"] as Style;
I want to set the Background color of an object's style, to be the color of the Window Foreground. So.... how to get the color of one object and use it as the value in a style?
<Setter Property="Background" Value="????Window Foreground Color????" />
I've tried different binding combinations, but none of them have worked yet. In code-behind this value would be this.Foreground, but in XAML style?
Quickest way would be to give you parent Window a name and use that in binding. Something like this (if you named your window "Root"):
<Setter Property="Background" Value="{Binding ElementName=Root, Path=Foreground}" />
I have a ListBox bound to a list of Items (for arguement, lets say its got a string and two dates Entered and Done).
I would like to make the background color of items in the ListBox have a gray color if the Done DateTime is != DateTime.MinValue.
Edit:
Should I make a converter? and convert DateTime to a Brush based on the value of the DateTime?
Is something like this my best option? or is there a simple Xaml snippet I could use?
[ValueConversion(typeof(DateTime), typeof(Brush))]
class MyConverter : IValueConverter
{
...
}
A ValueConverter would work. Another option would be to use a DataTrigger in the style of ListBoxItem. Maybe something like this:
<Style x:Name="MinDateTimeListBoxStyle" TargetType="ListBoxItem">
<Style.Triggers>
<Setter Property="Background" Value="Gray" />
<DataTrigger Binding="{Binding Path=Done}"
Value="{x:Static sys:DateTime.MinValue}">
<Setter Property="Background" Value="White" />
</DataTrigger>
</Style.Triggers>
</Style>
This will set the background to Gray when the value of Done isn't DateTime.MinValue. I don't think there is a way to do a not equals comparison in a trigger, so it sets the background to Gray by default, and only changing it back to white if Done hasn't changed yet. It would probably be better to use the correct color for the background instead of white (maybe get the value of the parent's background?), but this should give you something to start with.
Update: To apply this style to the items of only certain ListBoxes, give the style a name and set the ItemContainerStyle as appropriate:
<ListBox x:Name="StyledListBox"
ItemContainerStyle="{StaticResource MinDateTimeListBoxStyle}" />
<ListBox x:Name="NormalListBox" />