I have a text box for user to enter 6 character hex color value, a validator & a converter attached to it. Up to here, everything works fine. But I want to bind a textbox's Background
color to the color specified by the text box (ElementName
s Background
& Foreground
), and it does not seem to work.
When I debug/step through the code, the value seems like is always ""
XAML
<TextBox x:Name="Background" Canvas.Left="328" Canvas.Top="33" Height="23" Width="60">
<TextBox.Text>
<Binding Path="Background">
<Binding.ValidationRules>
<validators:ColorValidator Property="Background" />
</Binding.ValidationRules>
<Binding.Converter>
<converters:ColorConverter />
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
<TextBlock Canvas.Left="403" Canvas.Top="12" Text="Foreground" />
<TextBox x:Name="Foreground" Canvas.Left="403" Canvas.Top="33" Height="23" Width="60">
<TextBox.Text>
<Binding Path="Foreground">
<Binding.ValidationRules>
<validators:ColorValidator Property="Foreground" />
</Binding.ValidationRules>
<Binding.Converter>
<converters:ColorConverter />
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
<!-- in this example I used the converter used in the TextBox & another converter that converts a string to color -->
<TextBox ...
Background="{Binding ElementName=Background, Path=Text, Converter={StaticResource colorConverter}}"
Foreground="{Binding ElementName=Foreground, Path=Text, Converter={StaticResource stringToColorConverter}}" />
Converters
class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
string entry = ((Color)value).ToString();
return entry.Substring(3);
} catch (Exception) {
return Binding.DoNothing;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string entry = (string)value;
Validators.ColorValidator validator = new Validators.ColorValidator();
if (!validator.Validate(entry, System.Globalization.CultureInfo.CurrentCulture).IsValid) {
return Binding.DoNothing;
开发者_高级运维 }
return (Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF" + entry);
}
}
class StringToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string entry = (string)value;
Validators.ColorValidator validator = new Validators.ColorValidator();
if (!validator.Validate(entry, System.Globalization.CultureInfo.CurrentCulture).IsValid)
{
return Binding.DoNothing;
}
return (Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF" + entry);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Everyone saying that you need a brush and not a color is right.
Solution: Create another converter that returns a SolidColorBrush and you will be golden.
This May help who works in Asp.net technology. Which is used to set the Forecolor and Backcolor of the Textbox when bind the value of Textbox inside GridView. I am using this in .Net Version 4.6, I hope it support for other versions too.. Thank You
<asp:GridView ID="GridViewStatus" runat="server">
<Columns>
<asp:TemplateField HeaderText="Status" ItemStyle-Width="100px" >
<ItemTemplate>
<asp:Label ID="lbljobstatusAll" runat="server"
Text="<%#Eval("Status") %>" ForeColor='<%# Eval("Status").ToString().Contains("Yes") ? System.Drawing.Color.Green : System.Drawing.Color.Blue %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
精彩评论