【WPF】创建带CheckBox的ListBox


<StackPanel x:Class="UftTestClient.TestCaseView"
        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"
        mc:Ignorable="d"
        Height="811.732" Width="1046.107" Background="{DynamicResource {x:Static SystemColors.InactiveCaptionBrushKey}}">
    <StackPanel DockPanel.Dock="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid>
            <Button x:Name="BtAddToScene" Content="加入场景>>" Height="40" VerticalAlignment="Top" Width="88" Margin="389,91,399,0" Click="BtAddToScene_Click"/>
            <Button x:Name="BtDelFromScene" Content="<<移出场景" Height="40" VerticalAlignment="Top" Width="88" Margin="389,164,399,0" Click="BtDelFromScene_Click"/>
            <GroupBox x:Name="GpBxTestCases" Header="接口用例" HorizontalAlignment="Left" Height="255" Margin="10,23,0,0" VerticalAlignment="Top" Width="431">            
                <ListBox x:Name="LtBxTestCases" Margin="0,10,55,-2">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsSelected}" Content="{Binding TestCaseInfo}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </GroupBox>
            <GroupBox x:Name="GpBxTestScene" Header="场景用例" HorizontalAlignment="Left" Height="255" Margin="595,23,0,0" VerticalAlignment="Top" Width="408">
                <ListBox x:Name="LtBxTestCases1" HorizontalAlignment="Left" Margin="0,10,-2,-2" Width="398"/>
            </GroupBox>
            <Button x:Name="BtOpenTestCaseFile" Content="..." Margin="387,60,615,730" Click="BtOpenTestCaseFile_Click"/>
        </Grid>
    </StackPanel>
</StackPanel>

代码:
public partial class TestCaseView : StackPanel
    {
        public TestCaseView()
        {
            InitializeComponent();

            // 初始化字典容器
            m_ltStTestCases = new List<StTestCase>();
        }

        private void BtOpenTestCaseFile_Click(object sender, RoutedEventArgs e)
        {
            // 打开测试用例文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择测试用例文件";
            openFileDialog.Filter = "xml文件|*.xml";
            openFileDialog.FileName = string.Empty;
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.DefaultExt = "xml";

            if (openFileDialog.ShowDialog() == false)
            {
                return;
            }

            // 读取测试用例文件XML
            string strTestCaseFile = openFileDialog.FileName;
            XDocument document = XDocument.Load(strTestCaseFile);
            XElement root = document.Root;
            XElement testElement = root.Element("Test");

            IEnumerable<XElement> subElements = testElement.Elements();
            foreach (XElement subElement in subElements)
            {
                StTestCase stTestCase = new StTestCase
                {
                    strID = subElement.Attribute("id").Value,
                    strNote = subElement.Attribute("note").Value,

                    strSystem = subElement.Element("route").Attribute("system").Value,
                    strBranch = subElement.Element("route").Attribute("branch").Value,
                    dictInparams = new Dictionary<string, string>()
                };

                IEnumerable<XElement> inparamsElements = subElement.Element("inparams").Elements();
                foreach (XElement inElement in inparamsElements)
                {
                    string strName = inElement.Attribute("name").Value;
                    string strValue = inElement.Attribute("value").Value;
                    stTestCase.dictInparams.Add(strName, strValue);
                }

                m_ltStTestCases.Add(stTestCase);
            }

            // 更新用例ComboBox内容
            var testCaseInfo = new ObservableCollection<CTestCaseInfo>();
            foreach (var varStTestCase in m_ltStTestCases)
            {
                testCaseInfo.Add(new CTestCaseInfo{ IsSelected = false, TestCaseInfo = varStTestCase.strID + " " + varStTestCase.strNote });
            }
            this.LtBxTestCases.ItemsSource = testCaseInfo;
        }

        private void BtAddToScene_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("加入场景");
        }

        private void BtDelFromScene_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("移出场景");
        }

        // 成员变量
        private List<StTestCase> m_ltStTestCases;   // 接口用例
        private Dictionary<string, List<StTestCase>> m_dicScenes;   // 场景用例
    }

    public class CTestCaseInfo
    {
        public bool IsSelected { get; set; }
        public string TestCaseInfo { get; set; }
    }

 效果:

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM