J'ai une zone de texte pour que l'utilisateur entre une valeur de couleur hexadécimale de 6 caractères, un validateur & un convertisseur attaché à lui. Jusqu'à ici, tout fonctionne bien. Mais je veux lier la couleur Background
d'une zone de texte à la couleur spécifiée par la zone de texte (ElementName
s Background
& Foreground
), et cela ne semble pas fonctionner.Comment se lier à TextBox Couleur d'arrière-plan
Quand je debug/étape dans le code, la valeur semble être est toujours ""
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}}" />
Convertisseurs
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();
}
}
Je ne suis pas sûr, donc un commentaire, mais peut-être le problème est que TextBox.Background etc ont tous besoin d'un pinceau, pas d'une couleur. – Jens
Un meilleur titre aiderait sûrement à recueillir l'attention appropriée. – dhill
Comme Jens l'a dit, essayez de convertir en un pinceau plutôt qu'en couleur – snurre