【问题标题】:How to access Button programatically if the button is inside using C#如果按钮在内部,如何使用 C# 以编程方式访问按钮
【发布时间】:2009-07-16 10:25:17
【问题描述】:

由于我是第一次使用 XAML,所以我有几个问题。

  1. 如何使用按钮 (BrowseButton) 浏览硬盘中的文件夹? 在这种情况下,因为按钮在里面

  2. 我可以使用下面显示的方式吗? 实际上,第一个停靠面板将保存图像和一些标签,而另一个停靠面板将在其中包含 tabcontrol。

  3. 如果我有一个选项卡控件,我如何添加一个可以在运行时增加选项卡的列表视图......并且该列表视图在运行时也应该可用。 如何在选项卡顶部添加关闭(“X”)标记以关闭按钮

可能我问了很多问题,抱歉:(

请帮忙

<Grid>
        <DockPanel>
            <StackPanel>
                <Image Name="imgClientPhoto" HorizontalAlignment="Left" VerticalAlignment="Top" Width="auto" Height="auto" Grid.Column="0" Grid.Row="0" Margin="0" 
                       Source="D:ehtmp_top_left.gif" MinWidth="450" MinHeight="100" Grid.IsSharedSizeScope="True">

                </Image>

                 <Button x:Name="BrowseButton" Margin="0,13.638,30,14.362" Content="Browse" Click="BrowseButton_Click" HorizontalAlignment="Right" Width="111" />
                            <TextBox x:Name="txtBxBrowseTB" Margin="46,10,146,10" Text="TextBox" TextWrapping="Wrap" TextChanged="BrowseTB_TextChanged"></TextBox>
                            <Label HorizontalAlignment="Left" Margin="-14,22,0,10" Name="label1" Width="69.75" FontSize="13" VerticalContentAlignment="Top" HorizontalContentAlignment="Center">Path:</Label>
                        </Grid>
                    </GroupBox>
                </Grid>

            </StackPanel>

        </DockPanel>
        <DockPanel>
            <StackPanel Margin="0,158,0,0" HorizontalAlignment="Left" Width="330" Height="557.5" VerticalAlignment="Top">
                <TextBox Height="50" Name="textBox1" Width="240" Margin="0,35,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Right" />
                <ListBox Height="470" Name="listBox1" Width="332" Background="LightGray" Margin="0,0,0,0" BorderBrush="IndianRed" BorderThickness="3" />
            </StackPanel>
            <TabControl Height="234" Name="tabControl1" Width="1035" Margin="0,0,0,0">
                <TabItem Header="tabItem1" Name="tabItem1">
                    <Grid Height="144" />
                </TabItem>
            </TabControl>
        </DockPanel>
    </Grid>

【问题讨论】:

    标签: wpf listview button tabcontrol


    【解决方案1】:

    1) 浏览代码应该是这样的:

    private void BrowseButton_Click(object sender, RoutedEventArgs e)
    {
        System.Windows.Forms.FolderBrowserDialog browse = new System.Windows.Forms.FolderBrowserDialog();
        browse.RootFolder= Environment.SpecialFolder.MyDocuments;
        browse.SelectedPath = "C:\\InitalFolder\\SomeFolder";
        if (browse.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            txtBxBrowseTB.Text = browse.SelectedPath;
        }
    }
    

    2) 我试图简化 xaml。这看起来像你可以使用的东西吗?:

    <Grid>
        <Image Name="imgClientPhoto" Margin="0" Height="262" VerticalAlignment="Top" HorizontalAlignment="Left" ></Image>
        <StackPanel VerticalAlignment="Stretch" >
            <StackPanel VerticalAlignment="Top" Orientation="Horizontal" Height="30">
                <Button Name="BrowseButton" Content="Browse" Click="BrowseButton_Click" HorizontalAlignment="Right" Width="111" />
                <Label Name="label1" Width="69.75" FontSize="13" VerticalContentAlignment="Center" HorizontalContentAlignment="Right">Path:</Label>
                <TextBox Name="txtBxBrowseTB" Width="200" Text="TextBox" VerticalContentAlignment="Center" TextWrapping="Wrap" TextChanged="BrowseTB_TextChanged"></TextBox>
            </StackPanel>
            <StackPanel>
                <StackPanel Orientation="Vertical">
                    <TextBox Height="50" Name="textBox1"  />
                    <ListBox Height="470" Name="listBox1" Background="LightGray" Margin="0,0,0,0" BorderBrush="IndianRed" BorderThickness="3" MouseLeftButtonUp="listBox1_MouseLeftButtonUp">
                        <ListBoxItem>User1</ListBoxItem>
                        <ListBoxItem>User2</ListBoxItem>
                        <ListBoxItem>User3</ListBoxItem>
                        <ListBoxItem>User4</ListBoxItem>
                    </ListBox>
                </StackPanel>
                <TabControl Name="tabControl1" />
            </StackPanel>
        </StackPanel>
    </Grid>
    

    您还可以在选项卡项 Header 中有一个关闭按钮。与 xaml 控件中的许多内容属性一样,内容实际上可以是控件,而不仅仅是文本。

            <TabControl Name="tabControl1"  >
                <TabItem Name="tabItem1" >
                    <TabItem.Header>
                        <StackPanel Orientation="Horizontal">
                            <Label>TabHeader1</Label>
                            <Button Height="20" Width="20" FontWeight="Bold" Click="CloseTabButton_Click">X</Button>
                        </StackPanel>
                    </TabItem.Header>
                    <Grid Height="144">
                        <ListView />
                    </Grid>
                </TabItem>
            </TabControl>
    

    3) 以下是在 C# 中动态添加选项卡的方法:

    private void listBox1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        //Get selected item
        ListBoxItem item = (ListBoxItem)listBox1.SelectedItem;
        string itemText = item.Content.ToString();
    
        //Check if item is already added
        foreach (TabItem tItem in tabControl1.Items)
        {
            if ((string)tItem.Tag == itemText)
            {
                //Item already added, just activate the tab
                tabControl1.SelectedItem = tItem;
                return;
            }
        }
    
        //Build new tab item
        TabItem tabItem = new TabItem();
        tabItem.Tag = itemText;
    
        //First build the Header content
        Label label = new Label();
        Button button = new Button();
        label.Content = itemText;
        button.Content = "X";
        button.Height = 20;
        button.Width = 20;
        button.FontWeight = FontWeights.Bold;
        button.Click += CloseTabButton_Click; //Attach the event click method
        button.Tag = tabItem; //Reference to the tab item to close in CloseTabButton_Click
        StackPanel panel = new StackPanel();
        panel.Orientation = Orientation.Horizontal;
        panel.Children.Add(label);
        panel.Children.Add(button);
        tabItem.Header = panel;
    
        //Then build the actual tab content
        //If you need only the ListView in here, you don't need a grid
        ListView listView = new ListView();
        //TODO: Populate the listView with what you need
        tabItem.Content = listView;
    
        //Add the finished tabItem to your TabControl
        tabControl1.Items.Add(tabItem);
        tabControl1.SelectedItem = tabItem; //Activate the tab
    }
    
    private void CloseTabButton_Click(object sender, RoutedEventArgs e)
    {
        //The tab item was set to button.Tag when it was added
        Button button = (Button)sender;
        TabItem tabItem = (TabItem)button.Tag;
        if (tabItem != null)
        {
            button.Click -= CloseTabButton_Click; //Detach event handler to prevent memory leak
            tabControl1.Items.Remove(tabItem);
        }
    }
    

    【讨论】:

    • 嗨,我正在使用 C#。 (主题列出了 C# :))我对这个 WPF 和 XAML 非常陌生,所以无法弄清楚如何处理这个问题。请帮忙
    • 嗨,敬畏,对不起,如果我让你感到困惑。我的要求:如果我在列表框中有 4 个用户并且如果我点击他们,那么应该打开一个新选项卡。因此,对于每个新选项卡,都应添加 Listview(此 Listview 对所有选项卡都是通用的)。选项卡上的“X”标记也可以关闭选项卡。因此,Listview 将包含 1.Workbook、2.Desc.. 等等作为列。希望我有点道理。如果你需要,我可以解释。谢谢拉姆
    • 嗨,Dreas,我编辑了这个问题。 1. 一个基本的问题:如果按钮在 里面又在 里面存在,那么访问按钮的方式将保持不变??我的意思是
    • 访问按钮的最简单方法是在按钮上设置名称。然后就可以直接在代码中使用名称访问了。
    • 根据我对您问题的理解,我认为我的回答现在已经完成。你现在了解这些概念了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 2019-05-07
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多