使用EnumVisibilityConverter从一个用户控件转移到另一个-WPF

时间:2018-08-11 20:47:34

标签: c# wpf mvvm enums visibility

当用户按下按钮时,我想显示一个新的UserControl。我已经创建了一个EnumVisibilityConverter,并希望设置枚举以在按下按钮时显示另一个名为login的用户控件。到目前为止,我已经将enum属性重新分配为Login枚举类型,但是如何获取它以显示新的UserControl

这是我到目前为止的代码:

EnumToVisibilityConverter

public class EnumToVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// The converter will return Visibility.Visible if the parameter argument contains a string equal to value, Visible.Collapsed otherwise.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // value is null, parameter is null or value is not an enum type
            if(value == null || parameter == null || !(value is Enum))
            {
                return Visibility.Collapsed;
            }

            string currentState = value.ToString();
            string stateString = parameter.ToString();
            bool found = false;

            foreach(string state in stateString.Split(','))
            {
                found = (currentState == state.Trim());
                if(found)
                    break;
            }

            return found ? Visibility.Visible : Visibility.Collapsed;


        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Register.Xaml

<Window.Resources>
        <vm:RegisterViewModel x:Key="RegisterViewModelObject" />
        <enum:EnumToVisibilityConverter x:Key="enumToVisibility" />
</Window.Resources>

<StackPanel Background="{StaticResource LightBlueBrush}"
                Visibility="{Binding CurrentResponse, 
                Converter={StaticResource enumToVisibility}, 
                ConverterParameter='Register'}">

<!-- Move To Login Page -->
        <Button Content="I Already Have An Account"
                Margin="230 0 230 0"          
                Style="{StaticResource ChangeLoginPage}" />
    </StackPanel>

ResponseState(枚举类)

public enum ResponseState
{
    Login,
    Register
}

0 个答案:

没有答案