1. 程式人生 > >Revit二次開發之WPF設定CheckBox不能被選中【附原始碼】

Revit二次開發之WPF設定CheckBox不能被選中【附原始碼】

版本:

VS2015

Revit2018

實現功能:

WPF中選中一個CheckBox1後,利用Checked事件,另一個特定的CheckBox2會變成不可選狀態(XXX.IsEnabled = false);

取消選擇CheckBox1後,利用UnChecked事件,特定的CheckBox2會變成可選狀態(XXX.IsEnabled = true);

使用WPF的Checked、UnChecked事件

效果演示:

選中CheckBox1:


取消選擇CheckBox1:


原始碼:

WPF

<Window x:Class="JYCheckBox.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:JYCheckBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <CheckBox x:Name="num1" Content="CheckBox1" HorizontalAlignment="Left" Margin="38,0,0,268" VerticalAlignment="Bottom" Checked="checkBox_Checked" Unchecked="CheckBox_UnChecked"/>
        <CheckBox x:Name="num2" Content="CheckBox2" HorizontalAlignment="Left" Margin="268,36,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Unchecked="CheckBox_UnChecked"/>
        <CheckBox x:Name="num3" Content="CheckBox3" HorizontalAlignment="Left" Margin="268,73,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Unchecked="CheckBox_UnChecked"/>
        <CheckBox x:Name="num4" Content="CheckBox4" HorizontalAlignment="Left" Margin="268,108,0,0" VerticalAlignment="Top" Checked="checkBox_Checked" Unchecked="CheckBox_UnChecked"/>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JYCheckBox
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void checkBox_Checked(object sender, RoutedEventArgs e)
        {
            //System.Windows.Controls.CheckBox是為了避免和System.Windows.Forms發生衝突
            if (((System.Windows.Controls.CheckBox)(sender)).Name == "num4")
            {
                MessageBox.Show("選中特殊項");
            }

            //取消num1的選擇,也不能改變num2的不可選狀態
            if (((System.Windows.Controls.CheckBox)(sender)).Name == "num1")
            {
                num2.IsEnabled = false;
                return;
            }
        }

        private void CheckBox_UnChecked(object sender, RoutedEventArgs e)
        {
            //System.Windows.Controls.CheckBox是為了避免和System.Windows.Forms發生衝突
            if (((System.Windows.Controls.CheckBox)(sender)).Name == "num4")
            {
                MessageBox.Show("注意,您已取消特殊項");
            }

            //取消num1的選擇,也不能改變num2的不可選狀態
            if (((System.Windows.Controls.CheckBox)(sender)).Name == "num1")
            {
                num2.IsEnabled = true;
                return;
            }
        }

    }
}


C#

using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;

namespace JYCheckBox
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            MainWindow  nn=new MainWindow();
            nn.ShowDialog();
            return Result.Succeeded;
        }
    }
}