Reputation: 1519
I have settings for a list of buttons that show recent project file paths at the start up of the application. At this moment in time, file paths that still exist are still clickable, and file paths that are not found are not clickable. However, I want to modify the existing code such that nonexistent files paths are disguishable and have a different background color. How would I do this elegantly?
<Grid.Resources>
<Style TargetType="Button" x:Key="OpenProjectButtonStyle">
<Setter Property="Command" Value="{Binding Command, Converter={StaticResource ObjectToCommandConverter}}" />
<Setter Property="ToolTip" Value="{Binding ToolTipDescription}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="ButtonWrapper" Background="Transparent" Cursor="Hand">
<StackPanel Orientation="Horizontal" Margin="5">
<Image Source="{Binding LargeImageUrl}" Stretch="None" Margin="0,0,5,0" />
<StackPanel>
<TextBlock Name="LabelText" Text="{Binding Label}"
Foreground="{StaticResource SelectedForeground}"
FontSize="13" />
<TextBlock Text="{Binding ToolTipDescription}"
Foreground="#616161"
FontSize="11" />
</StackPanel>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="ButtonWrapper" Property="Background" Value="{StaticResource Background}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
I'm assuming I want to do a binding/conversion ilke one of the first few Setters, but I'm still not sure how. And this is what I have so far in my conversion class:
class CommandEnabledToBackgroundConverter : IValueConverter
{
public String Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool) value) //if command.Enabled is true
{
return "Gray";
}
else
{
return "LightGray";
}
}
public bool ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value.Equals("Gray")) //if command.Enabled is true
{
return true;
}
else
{
return false;
}
}
}
Upvotes: 2
Views: 159
Reputation: 20764
You need to return the brushes instead of strings:
class CommandEnabledToBackgroundConverter : IValueConverter
{
public String Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool) value) //if command.Enabled is true
{
return new SolidColorBrush(Colors.Gray);
}
else
{
return new SolidColorBrush(Colors.LightGray);
}
}
public bool ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The strings you normally use in XAML are implicitly converted to brushes, but for a converter this doesn't happen
Upvotes: 2
Reputation: 3290
I would just use a trigger, Your converter is correct, but there is no need to use one in this case.
<Trigger Property=Enabled Value=True>
<Trigger.Setters>
<Setter Property=Background Value=Gray />
</Trigger.Setters>
</Trigger>
Then just set the default background color to LightGray.
Upvotes: 0