开发者

XAML Toggle visibility among multiple options

开发者 https://www.devze.com 2023-02-15 00:10 出处:网络
On one of the screens in my WPF application, I have an enumerated list of helpful tips to show the user. Each of these tip messages contains complex markup, so I\'d like to keep the message definition

On one of the screens in my WPF application, I have an enumerated list of helpful tips to show the user. Each of these tip messages contains complex markup, so I'd like to keep the message definitions in XAML. I should only show one of these tips at a time.

How can I toggle the visibility of these messages based on my enum?

What I'm currently doing looks like this:

        <ContentControl Grid.Row="1">
            <ContentControl.Style>
                <Style TargetType="{x:Type ContentControl}">
                    <Setter Property="Visibility" Value="Collapsed"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Suggestion}" Value="AddDescription">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>
            <Border Style="{StaticResource SuggestBox}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Image Style="{StaticResource SuggestImage}"/>
                    <TextBlock Grid.Column="1" Style="{StaticResource SuggestMessage}">
                        You can add a description to this bookmark. Adding a description will make it easier to find.
                        <Hyperlink Command="{Binding EditCommand}">Add a description.</Hyperlink>
                    </TextBlock>
                </Grid>
            </Border>
        </ContentControl>

In the code above, Suggestion is an enum with values that correspond to the tip that ought to be displayed. I've only shown the XAML for one tip. Each additional tip has its own content control, with its style trigger bound to a d开发者_C百科ifferent value of the Suggestion enum.

This seems like a lot of XAML to support a simple visibility toggle operation. Surely there's an easier or better way?

(Note that the reason I can't apply the visibility trigger directly to the Border element is that the Border element already has a style set. Apparently, WPF doesn't allow you to combine styles.)

UPDATE: Just realized that this is essentially the same question as WPF Visibility of a UI element based on combo selection and How to hide/show items in a stack panel? but in a different context. The answers to those questions seem about as ugly as my XAML.


Try using a Converter: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert.aspx

public class MyConverter : IValueConverter
{
    public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
    {
        Suggestion suggestion = (Suggestion)parameter;
        SomeParameter param = (SomeParameter )value;

        //Do your logical work here and return Visibility.Collapsed or Visibility.Visible.
    }
}

Call it like this:

<Setter Property="Visibility" Value="{Binding Suggestion, Converter={StaticResource MyConverter}, ConverterParameter={Binding SomeParameter}}"/>

Or something similar but that's the general idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消