stackpanel visibility based on label content not working - wpf

I have a stack panel that I want to make visible based on a label's content. Just not sure why it isnt working for me. What's highlighted in bold is what I want to hide. Any suggestion?
<StackPanel Orientation="Horizontal">
<Label Nane="lblCarrier" Content="{Binding Path=Carrier}" />
**<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Setter Property="Visibility" Value="Visible" />
<Style.Triggers>
<DataTrigger Binding="{Binding Content, ElementName=lblCarrier}" Value="">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<Label x:Name="lblCarrierGrade" Content="Grade Carrier:" />
<TextBox x:Name="txtCarrierGrade1" />
<TextBox x:Name="txtCarrierGrade2" />
</StackPanel>**

It could be that the Content is null rather than String.Empty.
You could try using TargetNullValue
<DataTrigger Binding="{Binding Content, ElementName=lblCarrier,TargetNullValue=''}" Value="">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>

Why not using a converter? Add a class file to you project like this:
class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.IsNullOrEmpty(value as string) ? Visibility.Hidden : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
In your Window definition add this:
xmlns:myNamespace="clr-namespace:[YourProjectName]"
Then somewhere in the resources add this
<myNamespace:VisibilityConverter x:Key="myConverter"/>
Now you can use it:
<Style TargetType="StackPanel">
<Setter Property="Visibility"
Value="{Binding Content, ElementName=lblCarrier,
Converter = {StaticResources myConverter}}"/>

Related

WPF converter for labels' content

I'm trying to override the output of a label, say it contained "Account" and a client wants account rendered as "Member" (so kind of think of this as a localisation converter?)
My Question; is this possible with "hardcoded" content? or MUST i create a static file containing all label content (with iNotifiyStatic of course)? *for binding?
xaml:
<Label Style="{StaticResource LabelLeft}" Content="Account Name:"></Label>
Resource File: Including all attempts made, from multiple sources heres the most meaningful one.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters ="clr-namespace:Company.Resources.Converters">
<converters:LocationLabelsConverter x:Key="LocationLabelsConverter" />
<Style x:Key="LabelLeft" TargetType="{x:Type Label}" >
<Setter Property="Margin" Value="10 0 0 0"></Setter>
<Setter Property="Height" Value="22"></Setter>
<Setter Property="Padding" Value="0 0 0 0"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
<!-- Att1 -->
<!--<Setter Property="TextBlock.Text" Value="{Binding RelativeSource={RelativeSource self},
Path=Content,
Converter={StaticResource LocationLabelsConverter}}"></Setter>-->
<!-- Att2 -->
<!--<Setter Property="Content">
<Setter.Value>
<Binding Path="Content" RelativeSource="{RelativeSource self}">
<Binding.Converter>
<converters:LocationLabelsConverter/>
</Binding.Converter>
</Binding>
</Setter.Value>
</Setter>-->
<!-- Att3 -->
<!--<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource self},
Path=Content,
Converter={StaticResource LocationLabelsConverter}}">
<Setter Property="Content" Value="Test123"/>
</DataTrigger>
</Style.Triggers>-->
</Style>
And here's the converter:
[ValueConversion(typeof(string), typeof(string))]
public sealed class LocationLabelsConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
if (value != null)
{
return "hello sweety";// (string)value; //The goal here in the end is to run it through a method to replace string with correct text.
}
else return null;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return (string)value;
}
}
you can apply converter like this:
<Label Content="{Binding Source='Account Name:', Converter={StaticResource LocationLabelsConverter}"/>

WPF : How should I change the style of a button when his context id changed?

I have two button styles as static resources. BtnStyleOpen and BtnStyleClose.
A button with Name="V001" and in .cs I Bound the context with an object
BtnV001.Content = content; In content object there is a status property.
I would like to change the style of the button when the status is changed.
My code is:
<Button x:Name="Btn001" Grid.Column="5" Grid.Row="7"
Click="BtnV_Click" MouseRightButtonUp="BtnV_MouseRightButtonUp"
Content="{Binding Path=Status, UpdateSourceTrigger=PropertyChanged}">
<Button.Triggers>
<Trigger Property="Content" Value=1>
<Setter Property="Style" Value="{StaticResource BtnStyleOpen}" />
</Trigger>
<Trigger Property="Content" Value=0>
<Setter Property="Style" Value="{StaticResource BtnStyleClose}" />
</Trigger>
</Button.Triggers>
</Button>
try bind it to the event that invoke the change. try use datatrigger.
something like this :
<ToggleButton x:Name="togglebutton_Testing">
<TextBlock x:Name="Textblock_Testing">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsChecked, ElementName=togglebutton_Testing}" Value="false">
<Setter Property="Text" Value="Open"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsChecked, ElementName=togglebutton_Testing}" Value="true">
<Setter Property="Text" Value="Close"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</ToggleButton>
You can implement a IValueConverter and apply it to the binding between Button.Style and DataContext.Status:
public class StatusToStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Status status)
{
if (status == "open")
{
return (Style) Application.Current.FindResource("BtnStyleOpen");
}
if (status == "close")
{
return (Style) Application.Current.FindResource("BtnStyleClose");
}
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
You the have to add an instance of the IValueConverter?to some resouce:
<Button.Resource>
<StatusToStyleConverter x:Key"StatusToStyleConverter" />
<Button.Resource>
Then apply the Style:
<Button Style="{Binding Status, Converter={StaticResource StatusToStyleConverter}} />
As you didn't share details of your Status property type, you probably have to modify the condition evaluation in the converter.

How do you handle a RelativeSource binding for a source that may or may not exist?

We have a control that may or may not be hosted in a popup control. In the case when it is, we want to set properties on the popup using RelativeSource and OneWayToSource bindings. In cases where it's not hosted in a popup, we want the binding to basically be ignored/do nothing.
Only thing I can think of is binding to self with a custom converter which internally walks the visual tree looking for the popup. If found, do the magic. If not, do nothing. But I'm wondering if it can be done purely with XAML binding syntax.
In cases where it's not hosted in a popup, we want the binding to basically be ignored/do nothing.
Since you have a control one can create a Boolean dependency property, a flag, which can trigger either one of two hidden controls which behaves in a specific way due to which way the boolean is set.
I would call this the standard way, for the control is not required to know anything about the consumer, the consumer specifies the state.
Or
to set properties on the popup using RelativeSource and OneWayToSource bindings.
Similar to above, with two distinct hidden controls but then have the a style look for a specific window and a specific property. Then either hide or show the controls depending on what is found:
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsPopup,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}"
Value="True">
<Setter Property="IsEnabled"
Value="True" />
</DataTrigger>
</Style.Triggers>
Try following code:
<Border>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="Exists"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=StackPanel}}" Value="{x:Null}">
<Setter Property="Text" Value="No stackpanel"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
It will display both "No stackpanel" and error in Output window. If you place StackPanel inside Border "Exists" will be displayed. Set anything you wish inside DataTrigger when condition is fulfilled.
In case you want to avoid receiving error:
<Window.Resources>
<local:IsParentTypePresentToBoolConverter x:Key="IsParentTypePresentToBoolConverter"/>
</Window.Resources>
<Border>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="No StackPanel"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource IsParentTypePresentToBoolConverter}}" Value="True">
<Setter Property="Text" Value="Stackpanel exists as parent"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
converter which detects whether such a type is present as a parent:
class IsParentTypePresentToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var isPresent = FindParent<StackPanel>((DependencyObject) value);
return isPresent != null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
private T FindParent<T>(DependencyObject child) where T : DependencyObject
{
var parentObject = VisualTreeHelper.GetParent(child);
if (parentObject == null) return null;
var parent = parentObject as T;
return parent?? FindParent<T>(parentObject);
}
}
And here you have more generic equivalent where you make a use of reflection in order to find parent type.
class IsParentTypePresentToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var method = GetType().GetMethod("FindParent").MakeGenericMethod(new Type[1] { (Type)parameter });
var foundObject = method.Invoke(this, new object[] { (DependencyObject)value });
return foundObject != null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public T FindParent<T>(DependencyObject child) where T : DependencyObject
{
var parentObject = VisualTreeHelper.GetParent(child);
if (parentObject == null) return null;
var parent = parentObject as T;
return parent ?? FindParent<T>(parentObject);
}
}
The only distinction in XAML is that you indicate searching type of object.
<Border>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="No StackPanel"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource IsParentTypePresentToBoolConverter},
ConverterParameter={x:Type Border}}" Value="True">
<Setter Property="Text" Value="Stackpanel exists as parent"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Border>

WPF - XAML - Data Trigger without Binding / Value only trigger

I have a grid of values in a DataGrid. In each cell I have one of 4 values, I want each value to have an associated colour.
In the past all my data triggers have been of the format;
<DataTrigger Binding="{Binding Status}" Value="R">
<Setter Property="xcdg:DataRow.Background" Value="Pink" />
</DataTrigger>
But I need something of the format;
<DataTrigger Binding="{Binding *}" Value="R">
<Setter Property="xcdg:DataRow.Background" Value="Pink" />
</DataTrigger>
So that any cell's value will be checked. The reason I can't use explicit bindings is that the number of columns and their names is dynamic, I use extend CustomTypeDescriptor to expose the columns and rows to enter the grid.
Thanks in advance!
Is it an option to use a ValueConverter in your binding? That way you could check in the converter which value it should return.
<DataTrigger Binding="{Binding Path=Content, RelativeSource={x:Static RelativeSource.Self}}" Value="Failure">
<Setter Property="xcdg:DataRow.Background" Value="Red" />
</DataTrigger>
I needed to use a Relative source.
Further to that what I ended up using was;
<xcdg:DataGridControl.Resources>
<c:ColorConverter x:Key="colorConverter" />
<Style TargetType="{x:Type xcdg:DataCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}}" Value="False">
<Setter Property="xcdg:DataCell.Background" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource colorConverter}}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsDirty, RelativeSource={RelativeSource AncestorType={x:Type xcdg:DataRow}}}" Value="True">
<Setter Property="xcdg:DataCell.Background" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource colorConverter}}" />
</DataTrigger>
</Style.Triggers>
</Style>
</xcdg:DataGridControl.Resources>
Where my Colour converter looks a bit like this;
[ValueConversion(typeof(DataCell), typeof(Brush))]
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var cell = (DataCell)value;
var content = (string)cell.Content;
if (content == null || cell.ParentRow.IsSelected)
{
return DependencyProperty.UnsetValue;
}
if (content == "Unknown")
{
return new SolidColorBrush(Colors.LightYellow);
}
...
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
}
My only problem now is that when I scroll my background colours aren't updated when the DataRows are reused.
I can't find an event to hook into when a DataRow is reused either...

Simple WPF IValueConverter and DataTrigger not working together

I've been having trouble using a value converter with a data trigger. In some of my code it seems like the DataTrigger's Path is being applied to the root element, rather than the element which the style applies to.
I created a simple test case, and I don't understand its behavior. I'm expecting the Button to turn red or blue depending on which value is being fed to the DataTrigger's converter, but the Button isn't being affected at all!
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SimpleWpfApplication"
x:Class="SimpleWpfApplication.SimpleUserControl"
ToolTip="UserControl ToolTip">
<UserControl.Resources>
<local:SimpleConverter x:Key="SimpleConverter" />
</UserControl.Resources>
<Button ToolTip="Button ToolTip">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=ToolTip, Converter={StaticResource SimpleConverter}}"
Value="Button ToolTip">
<Setter Property="Background" Value="Red" />
</DataTrigger>
<DataTrigger
Binding="{Binding Path=ToolTip, Converter={StaticResource SimpleConverter}}"
Value="UserControl ToolTip">
<Setter Property="Background" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</UserControl>
And a simple converter:
class SimpleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new InvalidOperationException("SimpleConverter is a OneWay converter.");
}
}
Why isn't Convert being called? Why doesn't the Button turn red or blue?
Found the answer in another StackOverflow question: What’s wrong with my datatrigger binding?
The answer is to add RelativeSource={RelativeSource Self} to the binding:
<DataTrigger Binding="{Binding Path=ToolTip,
RelativeSource={RelativeSource Self},
Converter={StaticResource SimpleConverter}}" />

Resources