Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从combox中选择项目?如何防止用户向某些文本框添加文本?_C#_Xaml_Windows 8_Windows Store Apps - Fatal编程技术网

C# 如何从combox中选择项目?如何防止用户向某些文本框添加文本?

C# 如何从combox中选择项目?如何防止用户向某些文本框添加文本?,c#,xaml,windows-8,windows-store-apps,C#,Xaml,Windows 8,Windows Store Apps,我正在开发一个windows应用商店应用程序,我有一个控制面板页面,用户可以在其中输入信息。我添加了一个组合框,用户可以在其中选择是医生还是患者(当他选择医生时,用户将填写所有信息,但选择患者时,他不必填写所有文本框) 我的问题是:如何从组合框中选择项目??当这个项目被选中时,我怎样才能淡出一些文本框,或者采取措施阻止他向这些文本框添加信息??谢谢你这不是一个正确的提问方式。你必须发布你到目前为止所拥有的东西,换句话说,在这里询问之前你尝试了什么。你必须明确你在完成某件事情的过程中所面临的问题

我正在开发一个windows应用商店应用程序,我有一个控制面板页面,用户可以在其中输入信息。我添加了一个组合框,用户可以在其中选择是医生还是患者(当他选择医生时,用户将填写所有信息,但选择患者时,他不必填写所有文本框)


我的问题是:如何从组合框中选择项目??当这个项目被选中时,我怎样才能淡出一些文本框,或者采取措施阻止他向这些文本框添加信息??谢谢你

这不是一个正确的提问方式。你必须发布你到目前为止所拥有的东西,换句话说,在这里询问之前你尝试了什么。你必须明确你在完成某件事情的过程中所面临的问题

看看这个

对不起,链接错了。在结尾添加字符时拼写错误。这是你的电话号码

在链接中,您可以看到事件
SelectionChanged=“ComboBox\u SelectionChanged”


如果您面临任何问题,请告诉我

如果您使用MVVM模式,而不是将代码放在代码后面,那么从长远来看,您的生活会更轻松。这样,您可以将所有逻辑放入
ViewModel
类中,并使用绑定将其附加到用户界面。下面是一个实现您所需的简短示例

让我们从视图模型开始:

public class ViewModel : INotifyPropertyChanged
{
    // this property will hold the selected value in combo box
    private UserType _userType;
    public UserType UserType
    {
        get { return _userType; }
        set
        {
            _userType = value;
            OnPropertyChanged();
        }
    }

    // this property will return all available values for combo box
    public IEnumerable<UserType> UserTypes
    {
        get { return Enum.GetValues(typeof (UserType)).Cast<UserType>(); }
    }

    // INotifyPropertyChanged must be implemented to notify the UI about changed properties
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
由于我希望基于我的
enum
值显示或隐藏控件,因此我将创建一个转换器,根据
enum
值返回正确的可见性值:

public class UserTypeVisibilityConverter : IValueConverter
{
    // I can set this property to define which enum value makes the controls visible
    // all other values make them invisible
    public UserType VisibleUserType { get; set; }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (!(value is UserType))
        {
            return value;
        }

        var userType = (UserType) value;

        return userType == VisibleUserType ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
现在我只需要把我认为所有的东西放在一起:

<Page
    x:Class="_21643537_ComboBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_21643537_ComboBox"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <!-- I'm setting my ViewModel class as DataContext for the view  -->
    <!-- now I can bind to its properties  -->
    <Page.DataContext>
        <local:ViewModel />
    </Page.DataContext>

    <Page.Resources>
        <!-- I instantiate  -->
        <!-- I defined that Doctor makes the controls visible -->
        <local:UserTypeVisibilityConverter x:Name="VisibilityConverter" VisibleUserType="Doctor" />
    </Page.Resources>

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="100">
        <!-- I'm binding both properties to the combo box -->
        <!-- TwoWay mode is needed to write values from UI back to ViewModel -->
        <ComboBox ItemsSource="{Binding UserTypes}" SelectedItem="{Binding UserType, Mode=TwoWay}" />
        <!-- I use the same property to set visibility -->
        <!-- converter is used to convert enum to visibility -->
        <TextBox Visibility="{Binding UserType, Converter={StaticResource VisibilityConverter}}" />
    </StackPanel>
</Page>


当选择医生时,
文本框
现在可见,否则将隐藏。

您可以根据用户选择的索引动态构建所有文本框

XAML:


现在,您可以为每个用户创建不同的界面。

代码在哪里。。在此之前,请检查此项。[ [ ]谢谢你的链接!!但第一个不起作用:/但我的观点不是从组合框中选择一个项目,我的观点是,选择这个项目后,我希望它阻止用户编辑一些文本框…到目前为止,我的代码没有问题要更正…我只需要一个代码,可以让我在出现错误时阻止用户编辑文本框n组合框中的项目已被选中感谢DamirI先生在我将问题发布到这里之前添加了此事件
public class UserTypeVisibilityConverter : IValueConverter
{
    // I can set this property to define which enum value makes the controls visible
    // all other values make them invisible
    public UserType VisibleUserType { get; set; }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (!(value is UserType))
        {
            return value;
        }

        var userType = (UserType) value;

        return userType == VisibleUserType ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
<Page
    x:Class="_21643537_ComboBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_21643537_ComboBox"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <!-- I'm setting my ViewModel class as DataContext for the view  -->
    <!-- now I can bind to its properties  -->
    <Page.DataContext>
        <local:ViewModel />
    </Page.DataContext>

    <Page.Resources>
        <!-- I instantiate  -->
        <!-- I defined that Doctor makes the controls visible -->
        <local:UserTypeVisibilityConverter x:Name="VisibilityConverter" VisibleUserType="Doctor" />
    </Page.Resources>

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="100">
        <!-- I'm binding both properties to the combo box -->
        <!-- TwoWay mode is needed to write values from UI back to ViewModel -->
        <ComboBox ItemsSource="{Binding UserTypes}" SelectedItem="{Binding UserType, Mode=TwoWay}" />
        <!-- I use the same property to set visibility -->
        <!-- converter is used to convert enum to visibility -->
        <TextBox Visibility="{Binding UserType, Converter={StaticResource VisibilityConverter}}" />
    </StackPanel>
</Page>
 <ComboBox x:Name="comb1" SelectionChanged="ComboBox_SelectionChanged">
    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (comb1.SelectedIndex)
        {
            case 0: //doc
                TextBox name = new TextBox { PlaceholderText = "name", Margin = new Thickness(10) };
                break;

            case 1: //patient 
                //create controls.
                break;