【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