简体   繁体   中英

Using MultiDataTrigger to test for 0 and a boolean in TextBox which has StringFormat in XAML

I have a TextBox bound to a decimal value which I want to format to not display decimal values if they are zero. At the same time I want to change the Background to a color if the property value is zero and another boolean property is true.

I have managed to achieve both but not together. If I remove the StringFormat from the Text Binding below the Background works perfectly but a value of, for example, 10 is displayed as 10.00, which I don't want. With the StringFormat the Background is not set and the TextBox is empty when 0 and suppresses decimals if they are zero, as required (empty string for zero is acceptable). So I assume it has to do with the empty string. I did try to test for an empty string with Path=Text.IsEmpty and value="True" as suggested in another post but that gives a design time error that Property IsEmpty is not found on type String.

This is what I have so far:

    <TextBox x:Name="FactorValueTextBox" Text="{Binding Value, StringFormat={}{0:# ###.##}}" IsReadOnly="{Binding CommodityGradingFactor.Total}" GotKeyboardFocus="GradeFactorGotKeyboardFocus">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}" Value="0"/>
                        <Condition Binding="{Binding CommodityGradingFactor.Manditory}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background" Value="LightPink"/>
                </MultiDataTrigger>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsMouseOver" Value="True"/>
                        <Condition Property="IsReadOnly" Value="False"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="FontSize" Value="15"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

For completeness the GotKeyboardFocus() function simply selects the current value when the user tabs into the TextBox :

private void GradeFactorGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    if (e.KeyboardDevice.IsKeyDown(Key.Tab))
        ((TextBox)sender).SelectAll();
}

Instead of binding the condition to the property Text

<Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text}" Value="0"/>

Why not binding it to the property Value

<Condition Binding="{Binding Value}" Value="0"/>

Note:

Depending on your use case, you may want to implement INotifyPropertyChanged on your Value property, avoid naming your backfield property as "value" since it is a Contextual keywords for INotifyPropertyChanged case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM