0

XAML

<TextBlock FontSize="14" Foreground="Red">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <!--Here I want to compare with an OR behavior-->
                        <Condition Binding="{Binding Username}" Value="" />
                        <Condition Binding="{Binding Username}" Value="{x:Null}"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Text" Value="No Username"/>
                    <Setter Property="Visibility" Value="Visible"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

For this to be triggered, Username has to be null and empty which make no sense. In fact, I want an OR behavior with these two values.

NOTE

I know that I could add multiple <DataTrigger> (like for example in MultiDataTrigger with OR instead of AND) and since they are handled in order it would be fine, but that's exactly what I'm trying to avoid. I'm looking more for a one-liner solution.

ASh
  • 34,632
  • 9
  • 60
  • 82
scharette
  • 9,437
  • 8
  • 33
  • 67
  • @Clemens The answer of the duplicate is exactly what I'm trying to avoid. Does that means it is impossible ? – scharette Jun 13 '18 at 19:30
  • Obviously, yes. – Clemens Jun 13 '18 at 19:31
  • @Clemens Hope it truly is.. The answer is 2 years old. – scharette Jun 13 '18 at 19:33
  • _If those answers do not fully address your question, please edit this question to explain how it is different or ask a new question._ What to do if I did initially ? – scharette Jun 13 '18 at 19:40
  • 1
    It is so happened that I'm the author of duplicate answer, but I have a shorter suggestion for this question, but it was closed before I could post the answer. Voting to reopen – ASh Jun 13 '18 at 19:43
  • @ASh That was my guess. But you know we're in 2018 now, I must not be offended by an impulsive closing... – scharette Jun 13 '18 at 19:45
  • @ASh Why don't you just edit your existing answer? Or is it a suggestion that doesn't make sense in the context of the other question? – Clemens Jun 13 '18 at 20:21
  • @Clemens, my suggestion won't work for old question. only for this one – ASh Jun 13 '18 at 20:30

2 Answers2

4

in case when DataTrigger should work for some value and for null, you can replace null for that value, using TargetNullValue atrribute of Binding.

TargetNullValue='' replaces null with an empty string.

<TextBlock FontSize="14" Foreground="Red">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Username, TargetNullValue=''}" Value="">
                    <Setter Property="Text" Value="No Username"/>
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

it is also possible to make a visibility converter specifically for empty/null strings and work without triggers:

<TextBlock FontSize="14" Foreground="Red" Text="No Username"
           Visibility="{Binding Username, Converter={StaticResource MyStringToVisibiltyConverter}}"/>

where Convert method will be smth like:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var str = value as string;
    return String.IsNullOrEmpty(str) ? Visibility.Visible : Visibility.Collapsed;
}
ASh
  • 34,632
  • 9
  • 60
  • 82
  • This is a really good answer and is worth way more attention. No other threads were talking about this specific problem. Really sad that @Clemens closed it since traffic will be significantly reduced. Anyway, thank you a lot for feedback ! – scharette Jun 14 '18 at 12:26
  • @scharette, glad I was able to help. would you mind if I add a link to former duplicate https://stackoverflow.com/questions/38396419/multidatatrigger-with-or-instead-of-and to your Question, so they show in Linked section and someone else with a similar issue could find them easier? – ASh Jun 14 '18 at 12:43
  • I'd say that would be ideal. Both question are related, but you answer a more specific problem here in my opinion. – scharette Jun 14 '18 at 12:46
1

Why not just do it in your binding

private string userName;
public string UserName 
{
    get { return string.IsNullOrEmpty(userName) ? null : userName; }
    set { userName = value; }
}
paparazzo
  • 44,497
  • 23
  • 105
  • 176