阻止用户在WPF C#

时间:2018-04-05 14:21:14

标签: c# wpf

以下是我的要求。

用户可以从ComboBox中选择任何一个选项。之后如果他试图从ComboBox中选择任何选项,我需要显示一个MessageBox,它将要求用户保存第一个选择的数据,而不清除之前的选择。

任何人都可以帮忙解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

有多种方法可以做到这一点。虽然詹姆斯在你的评论中提到他们错误地选择了一个糟糕的设计。除此之外,我们走了。

解决方案1 ​​

加入ComboBox事件SelectedValueChangedSelectedIndexChanged(预先使用Windows窗体)并在那里,只需执行((ComboBox)sender).Enabled = false;,以便他们无法更改。

保存上,然后重新启用它并清除选择。

解决方案2

仍允许用户选择并执行您要求的事情。执行以下操作:

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// The last unsaved selected item
        /// </summary>
        private object mLastSelectedValue;

        public Form1()
        {
            InitializeComponent();

            // Make it a fixed list
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

            // Add some dummy items
            comboBox1.Items.Add("Item 1");
            comboBox1.Items.Add("Item 2");
            comboBox1.Items.Add("Item 3");
            comboBox1.Items.Add("Item 4");
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the desired new value
            var newValue = ((ComboBox)sender).SelectedItem;

            // If value is the last-selected one...
            if (mLastSelectedValue == newValue)
                // Do nothing else
                return;

            // If this is a new selection
            if (mLastSelectedValue == null)
            {
                // Save the new selection
                mLastSelectedValue = newValue;
            }
            // Otherwise, the value is different...
            else
            {
                // Restore value
                ((ComboBox)sender).SelectedItem = mLastSelectedValue;

                // Show message box
                MessageBox.Show("Please save your work first");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // TODO: Save something
            MessageBox.Show("Saved");

            // Clear last value
            mLastSelectedValue = null;

            // Clear combo box selected item
            comboBox1.SelectedItem = null;
        }
    }
}

WPF解决方案

注意标题声明WPF ......所以这是WPF方式

<强>的Xaml

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <ComboBox x:Name="ComboBox1" SelectionChanged="ComboBox1_SelectionChanged">
        </ComboBox>

        <Button x:Name="SaveButton" Content="Save" Click="SaveButton_Click"></Button>

    </StackPanel>
</Window>

<强>代码隐藏

using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// The last unsaved selected item
        /// </summary>
        private object mLastSelectedValue;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            // TODO: Save something
            MessageBox.Show("Saved");

            // Clear last value
            mLastSelectedValue = null;

            // Clear combo box selected item
            ComboBox1.SelectedItem = null;
        }

        private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Get the desired new value
            var newValue = ((ComboBox)sender).SelectedItem;

            // If value is the last-selected one...
            if (mLastSelectedValue == newValue)
                // Do nothing else
                return;

            // If this is a new selection
            if (mLastSelectedValue == null)
            {
                // Save the new selection
                mLastSelectedValue = newValue;
            }
            // Otherwise, the value is different...
            else
            {
                // Restore value
                ((ComboBox)sender).SelectedItem = mLastSelectedValue;

                // Show message box
                MessageBox.Show("Please save your work first");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Add some dummy items
            ComboBox1.Items.Add("Item 1");
            ComboBox1.Items.Add("Item 2");
            ComboBox1.Items.Add("Item 3");
            ComboBox1.Items.Add("Item 4");
        }
    }
}

答案 1 :(得分:0)

使用组合框的select index changed事件并为所有可编辑字段维护一个布尔标志。如果出现问题,则将布尔标志设置为true,并在select index changed事件中检查此布尔值。如果在事件中为真,则显示消息。