Cannot find converter resource - wpf

I have a WPF Solution with 2 projects, the first one (ResourcesLibrary) contains common styles and common resources. The other is the WPF application.
I created a style for DataGridRows in the ResourcesLibrary generic.xaml file:
<Style x:Key="DGRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Image Source="/Resources/stop.png"
ToolTip = "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
Path=(Validation.Errors),
Converter={StaticResource errorConverter]}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
I added the .cs converter files to the ResourcesLibrary project:
namespace ResourceLibrary
{
public class ErrorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var errors = value as ReadOnlyObservableCollection<ValidationError>;
if (errors == null)
return "";
return errors.Count > 0 ? errors[0].ErrorContent : "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
And added the reference and the static resource:
xmlns:my="clr-namespace:ResourceLibrary"
<!-- CONVERTERS -->
<my:ErrorConverter x:Key="errorConverter" />
But at runtime when, in the main project, I use the DataGridRow style defined in the ResourcesLibrary I get this error:
{"Cannot find resource named 'errorConverter]'. Resource names are case sensitive."}
Do I need to have another project inside my solution for the converters I will use?

Thanks to #Clemens I removed the extra char ']' in the expression "={StaticResource errorConverter]}" and it all works fine.

Related

WPF underline binding (MVVM)

Is there a way to bind underline?? I'm trying to achieve the following:
I have VievModel with a bool property:
public bool HomeButtonUnderline { get; set; } = false;
I would then like to control this property in the following function:
public void Home() {
//CurrentPage = ApplicationPage.Home;
//HomeButtonForeground = new SolidColorBrush(Colors.White);
HomeButtonUnderline = true;
SettingsButtonUnderline = false;
}
I could then utilize this control in XAML:
<Button>
<TextBlock Command="{Binding HomeNavCommand}" Underline="{Binding HomeButtonUnderline}"/>
</Button>
The problem is that there isn't an 'Underline' property, instead it is handled by 'TextDecorations':
<Button>
<TextBlock TextDecorations="Underline">
</Button>
So is there a way to control underline using MVVM or even without it??
You may use a DataTrigger in a Style for the TextBlock:
<Button Command="{Binding HomeNavCommand}">
<TextBlock Text="Home">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding HomeButtonUnderline}" Value="True">
<Setter Property="TextDecorations" Value="Underline"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Button>
Use a Converter to convert your model types (in this case, a bool) to the UI types (in this case, a TextDecoration).
public class UnderlineConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return null;
return System.Convert.ToBoolean(value) ? TextDecorations.Underline : null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
And then use it in your binding (after creating a resource in your Window, UserControl App.xaml or whereever you feel is the right place; for this sample I'm just putting it in the button resources)
<Button Command="{Binding HomeNavCommand}">
<Button.Resources>
<local:UnderlineConverter x:Key="UnderlineConverter" />
</Button.Resources>
<TextBlock TextDecorations="{Binding HomeButtonUnderline, Converter={StaticResource UnderlineConverter}"/>
</Button>
Of course, another way to handle this is for your view model to be the right type for the view:
public TextDecorationCollection HomeButtonUnderline { get; set; }
HomeButtonUnderline is already GUI-orientated, so there's probably some code in your viewmodel that assigns a value from the model, so it might as well also convert from bool to TextDecorations.Underline.
This method keeps all the logic in the same file, unlike a converter, although a converter is more reusable. It also has the advantage of being easily unit-testable (unlike a data trigger).

wpf dynamic user security

The software uses a SmartSnowUser object, which contains a SecurityRole object. The client needs SecurityRole to be customizable, so it has a list of enum SecurityTasks, which the clients can add/remove from. Controls should only be visible if their given SecurityTask exists in the current SmartSnowUser's
SecurityRole.
With this setup, I am struggling to get all the functionality I need.
I need the ability to:
change control visibility based on whether CurrentUser.Role contains GivenTask
make control visibility more granular when necessary (e.g. Visibility &= isInEditMode)
meet the previous two requirements without having to create a separate style for each color/task/extra-qualifier combination
Here are the two main approaches I've tried.
Attempt #1:
Wpf User Security Strategy
Current issue: visibility is not being triggered; breakpoint in Convert() method is never hit
Long-term issue: Uses style, so every other custom style will need to be BasedOn this default; also, have to duplicate functionality for editability
Code:
/**Style.xaml**/
<local:TagToVisibilityConverter x:Key="TagToVisibilityConverter"/>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource TagToVisibilityConverter}">
<Binding Path="MainData.CurrentUser"/>
<Binding RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
/**Style.xaml.cs**/
public class TagToVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length >= 2 && (values[1] as FrameworkElement).GetValue(SecurityLevel.RequiredTaskProperty) is SecurityTask requiredTask)
{
//If element has a task assigned and user is not logged in, do not show
if (values[0] is SmartSnowUser currentUser && currentUser.Role != null)
{
return currentUser.Role.Tasks.Contains(requiredTask) ? Visibility.Visible : Visibility.Collapsed;
}
return Visibility.Collapsed;
}
//If element has no task assigned, default to visible
return Visibility.Visible;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class SecurityLevel
{
public static readonly DependencyProperty RequiredTaskProperty = DependencyProperty.RegisterAttached("RequiredTask", typeof(SecurityTask), typeof(FrameworkElement), new PropertyMetadata(SecurityTask.ControlBasic));
public static void SetRequiredTask(UIElement element, SecurityTask value)
{
element.SetValue(RequiredTaskProperty, value);
}
public static SecurityTask GetRequiredTask(UIElement element)
{
return (SecurityTask)element.GetValue(RequiredTaskProperty);
}
}
/**Implementation in User Control**/
<Button Name="BtnNew" Content="Create New Role" Style="{StaticResource ButtonBlue}" server:SecurityLevel.RequiredTask="{x:Static enums:SecurityTask.EditRoles}" />
Attempt #2:
How to extend instead of overriding WPF Styles
How to add dependency property to FrameworkElement driven classes in WPF?
Attempted to merge these two solutions into one. Set the Tag value to a SecurityTask, then use trigger to set visibility
issue: cannot set visibility at a more granular level without a style (e.g. cannot set 'Visibility' property directly in control); cannot distinguish between visibility/editability
Code:
/**Style.xaml**/
<!--#region Visibility-->
<!-- Default frameworkElement style definition -->
<local:TagToVisibilityConverter x:Key="TagToVisibilityConverter"/>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Visibility">
<Setter.Value>
<MultiBinding Converter="{StaticResource TagToVisibilityConverter}">
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}"/>
<Binding Path="MainData.CurrentUser"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
<!-- Extending default style -->
<Style x:Key="ButtonBasic" TargetType="Button" BasedOn="{StaticResource {x:Type FrameworkElement}}">
<Setter Property="Background" Value="{StaticResource BrushGreyDark}" />
<Setter Property="Foreground" Value="{StaticResource BrushWhite}" />
</Style>
<Style x:Key="ButtonBlue" TargetType="Button" BasedOn="{StaticResource ButtonBasic}">
<Setter Property="Background" Value="{StaticResource BrushBlue}" />
</Style>
/**Style.xaml.cs**/
public class TagToVisibilityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length >= 2 && values[0] is SecurityTask requiredTask)
{
//If element has a task assigned and user is not logged in, do not show
if (values[1] is SmartSnowUser currentUser && currentUser.Role != null)
{
return currentUser.Role.Tasks.Contains(requiredTask) ? Visibility.Visible : Visibility.Collapsed;
}
return Visibility.Collapsed;
}
//If element has no task assigned, default to visible
return Visibility.Visible;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
/**Implementation in User Control**/
//This button works great. Exactly what I need.
<Button Name="BtnNew" Content="Create New Role" Style="{StaticResource ButtonBlue}" Tag="{x:Static enums:SecurityTask.EditRoles}" />
//This button does not work, because the newly set Visibility property overrides the style.
<Button Name="BtnEdit" Content="Edit Role" Style="{StaticResource ButtonBlue}" Tag="{x:Static enums:SecurityTask.EditRoles}" Visibility="{Binding IsEditMode, Converter={StaticResource InverseBoolToVisibilityConverter}}" />
Attempt #2 ALMOST works. It's that last stinking button, BtnEdit. It is far too cluttered to create a new style - BasedOn ButtonBlue, which is BasedOn ButtonDefault, which is BasedOn our original up there - every time I need to add another qualifier to my visibility setting.
I seem to be over-complicating this. Is there a cleaner approach to what I'm trying to do?

Bind WPF Label visibility to entered password length

I'm trying to bind the visibility of a label to the typed length of a PasswordBox control. I'm binding the Visibility property of the label to the output of a converter targeted at the SecurePassword.Length property of the passwordbox.
The binding works fine, but only once, when the app first starts. It doesn't stay in sync. If I seed the password with a long enough value (as in my example), the message is displayed, but it doesn't update as I type or delete text. Clearly, I'm missing something.
I used this question as a template for my implementation.
My Xaml:
<PasswordBox Name="PwdPassword" HorizontalAlignment="Left" MinWidth="120"
Password="abcdefghijklmnopqrstuvwxyz1234567890"></PasswordBox>
<Label Name="LblPasswordMsg" Content="Message" FontWeight="Bold">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Visibility" Value="{Binding UpdateSourceTrigger=PropertyChanged, ElementName=PwdPassword, Mode=OneWay,
Path=SecurePassword.Length, Converter={StaticResource IntLengthVisibilityConverter}}" />
</Style>
</Label.Style>
</Label>
My Converter:
[ValueConversion(typeof(int), typeof(Visibility))]
public class IntLengthVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Do the conversion from int to visibility
int length = (int)value;
Visibility visible = length >= 25 ? Visibility.Visible : Visibility.Hidden;
return visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Do the conversion from visibility to int
return null;
}
}
I think the problem is SecurePassword.Length is not a dependency property, so no automatic notification occurs when its value changes.

Colouring a hierarchical XamDataGrid

I'm using a XamDataGrid (Infragistics-control) to display some hierarchical data. The objects that I can have up to 10 levels and I need to be able to give each level a specific background-color. I use the AssigningFieldLayoutToItem-event to get the "level" of the item and it would be best to assign the background/style here as well, I suppose.
I have tried specifying a DataRecordCellArea-style and even a CellValuePresenter-style but I can't get any of these to work with the FieldLayouts.
Another solution is to write a FieldLayout for each level, but this would create a lot of unnecessary XAML-code.
Any suggestions as to what I should do?
If you have a different FieldLayout for each level, you could use a single style targeting the DataRecordPresenter with a converter to set the background.
XAML:
<local:BackgroundConverter x:Key="BackgroundConverter"/>
<Style TargetType="{x:Type igDP:DataRecordPresenter}">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=FieldLayout.Key, Converter={StaticResource BackgroundConverter}}"/>
</Style>
Converter:
public class BackgroundConverter:IValueConverter
{
public BackgroundConverter()
{
this.Brushes = new Dictionary<string, Brush>();
}
public Dictionary<string, Brush> Brushes {get;set;}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is string)
{
string key = value.ToString();
if (this.Brushes.ContainsKey(key))
return this.Brushes[value.ToString()];
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
The following will set the colors to use for fields with Key1 and Key2:
BackgroundConverter backgroundConverter = this.Resources["BackgroundConverter"] as BackgroundConverter;
backgroundConverter.Brushes.Add("Key1", Brushes.Green);
backgroundConverter.Brushes.Add("Key2", Brushes.Yellow);
If you are reusing the same FieldLayout for multiple fields, then you could use the InitializeRecord event and change the style to bind to the Tag of the DataRecord like this:
XAML:
<Style TargetType="{x:Type igDP:DataRecordPresenter}">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Record.Tag}"/>
</Style>
C#:
void XamDataGrid1_InitializeRecord(object sender, Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs e)
{
if (!e.ReInitialize)
{
// Set the tag to the desired brush.
e.Record.Tag = Brushes.Blue;
}
}
Note that I didn't add the conditional logic for determining the brush to use and that still needs to be done for different levels to have different backgrounds.

Binding for WPF Styles

I'm trying to create a custom control - a button - which will have multiple styles applied to it depending on the value of a property within the data context.
What I was thinking is using something similar to:
<Button Style="{Binding Path=ButtonStyleProperty, Converter={StaticResource styleConverter}}" Text="{Binding Path=TextProp}" />
And in code... Implement an IValueConverter which does something similar to the code below in the ConvertTo method:
switch(value as ValueEnums)
{
case ValueEnums.Enum1:
FindResource("Enum1ButtonStyle") as Style;
break;
... and so on.
}
However I'm not entirely sure about how to pull out the style object and even if this is possible at all...
What I am doing in the mean time is handling the DataContextChanged event, then attaching a handler to the PropertyChanged event of the object being bound to the button - then running the switch statement in there.
Its not quite perfect but until I can find a better solution it seems like that is what I'll have to use.
If you want to replace the whole style (rather than just elements of it) then you'll probably be storing those styles in resources. You should be able to do something along the lines of:
<Button>
<Button.Style>
<MultiBinding Converter="{StaticResource StyleConverter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"/>
<Binding Path="MyStyleString"/>
</MultiBinding.Bindings>
</MultiBinding>
</Button.Style>
</Button>
By using a MultiBinding and using Self as the first binding we can then lookup resources in our converter. The converter needs to implement IMultiValueConverter (rather than IValueConverter) and can look something like this:
class StyleConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
FrameworkElement targetElement = values[0] as FrameworkElement;
string styleName = values[1] as string;
if (styleName == null)
return null;
Style newStyle = (Style)targetElement.TryFindResource(styleName);
if (newStyle == null)
newStyle = (Style)targetElement.TryFindResource("MyDefaultStyleName");
return newStyle;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
It's not something I do very often, but that should work from memory :)
It seems that you need to use DataTrigger class. It allows you to apply different styles to your button based on it's content.
For example following style will change button's background property to red based on value of data context object's property
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path="Some property"}"
Value="some property value">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
For those of us who can't use multi value converter (I'm looking at you SL4 and WP7:), thanks to Steven's answer I found a way using an ordinary value converter.
The only assumption is the style value is contained within the property of the style being set.
So if you're using the MVVM pattern then the style value (such as TextSmall, TextMedium, TextLarge) is assumed to be part of the view model, and all you have to do is pass the converter parameter defining the name of style.
For example, say your view model has property:
public string ProjectNameStyle
{
get { return string.Format("ProjectNameStyle{0}", _displaySize.ToString()); }
}
Application style:
<Application.Resources>
<Style x:Key="ProjectNameStyleSmall" TargetType="TextBlock">
<Setter Property="FontSize" Value="40" />
</Style>
<Style x:Key="ProjectNameStyleMedium" TargetType="TextBlock">
<Setter Property="FontSize" Value="64" />
</Style>
<Style x:Key="ProjectNameStyleLarge" TargetType="TextBlock">
<Setter Property="FontSize" Value="90" />
</Style>
XAML view:
<TextBlock
Text="{Binding Name}"
Style="{Binding ., Mode=OneWay, Converter={cv:StyleConverter}, ConverterParameter=ProjectNameStyle}">
With your StyleConverter class implementing IValueConverter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != typeof(Style))
{
throw new InvalidOperationException("The target must be a Style");
}
var styleProperty = parameter as string;
if (value == null || styleProperty == null)
{
return null;
}
string styleValue = value.GetType()
.GetProperty(styleProperty)
.GetValue(value, null)
.ToString();
if (styleValue == null)
{
return null;
}
Style newStyle = (Style)Application.Current.TryFindResource(styleValue);
return newStyle;
}
Note that this is WPF code, as the converter is derived from a MarkupExtension as well as IValueConverter, but it will work in SL4 and WP7 if you use static resource and add a bit more leg work as the TryFindResource method doesn't exist.
Hope that helps someone, and thanks again Steven!
ViewModel
private Style _dynamicStyle = (Style)Application.Current.FindResource("Style1");
public Style DynamicStyle
{
get { return _dynamicStyle; }
set
{
_dynamicStyle = value;
OnPropertyChanged("DynamicStyle");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Implement a property in your ViewModel and then dynamically change style where ever you want like below.
DynamicStyle=(Style)Application.Current.FindResource("Style2");// you can place this code where the action get fired
View
Then set DataContext value and then implement the following code in your view
<Button Style="{Binding DynamicStyle,Mode=TwoWay}"/>

Resources