WPF用户控件有两个版本

时间:2022-10-24 05:32:57

I need to create user control (or something else) that takes model and based on a property of that model it changes how it looks like. I need it to have minimized and extended versions of same item.

我需要创建采用模型的用户控件(或其他东西),并根据该模型的属性更改它的外观。我需要它具有相同项目的最小化和扩展版本。

some elements are missing and some change size. Most probably I need to define two data templates but I do not know how to use this.

一些元素缺失,一些变化大小。最有可能我需要定义两个数据模板,但我不知道如何使用它。

thanks in advance

提前致谢

1 个解决方案

#1


0  

Something like this should do you:

这样的事情应该对你有用:

<Grid>
        <Grid x:Name="firstView" Visibility="{Binding firstViewActive, Converter={StaticResource BooleanToVisibilityConverter}}">
            <!-- First view xaml goes here -->
        </Grid>
        <Grid x:Name="secondView" Visibility="{Binding firstViewActive, Converter={StaticResource InvertedBooleanToVisibilityConverter}}">
            <!-- Second view xaml goes here -->
        </Grid>
    </Grid>

You'd need to implement the converters, something like this:

您需要实现转换器,如下所示:

    public class BoolToVisConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;
        }
    }



    public class InvertedBoolToVisConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return !(value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return !(value is Visibility && (Visibility)value == Visibility.Visible);
        }
    }

#1


0  

Something like this should do you:

这样的事情应该对你有用:

<Grid>
        <Grid x:Name="firstView" Visibility="{Binding firstViewActive, Converter={StaticResource BooleanToVisibilityConverter}}">
            <!-- First view xaml goes here -->
        </Grid>
        <Grid x:Name="secondView" Visibility="{Binding firstViewActive, Converter={StaticResource InvertedBooleanToVisibilityConverter}}">
            <!-- Second view xaml goes here -->
        </Grid>
    </Grid>

You'd need to implement the converters, something like this:

您需要实现转换器,如下所示:

    public class BoolToVisConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value is Visibility && (Visibility)value == Visibility.Visible;
        }
    }



    public class InvertedBoolToVisConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return !(value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return !(value is Visibility && (Visibility)value == Visibility.Visible);
        }
    }