Wpf 如果按钮在内部,如何使用C编程访问按钮#

Wpf 如果按钮在内部,如何使用C编程访问按钮#,wpf,listview,button,tabcontrol,Wpf,Listview,Button,Tabcontrol,我有几个问题,因为我是第一次使用XAML 如何使用按钮(BrowseButton)浏览硬盘中的文件夹? 在这种情况下,因为按钮在里面 我可以使用下面显示的方式吗? 实际上,第一个dockpanel将保存图像和一些标签,而另一个dockpanel将在其中包含tabcontrol 如果我有一个tabcontrol,如何添加一个可以在运行时增加选项卡的listview。。listview也应该在运行时可用。 如何在选项卡顶部添加关闭(“X”)标记以关闭按钮 可能我问了很多问题,对不起:( 请帮忙 &l

我有几个问题,因为我是第一次使用XAML

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

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

  • 如果我有一个tabcontrol,如何添加一个可以在运行时增加选项卡的listview。。listview也应该在运行时可用。 如何在选项卡顶部添加关闭(“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>
    
    
    路径:
    
    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>
    

    嗨,我用的是C。(主题已列出:))我对WPF和XAML非常陌生,因此无法确定如何处理此问题。请帮助我,抱歉,如果我把你弄糊涂了。我的请求:如果我在列表框中有4个用户,如果我点击他们,那么应该打开一个新的选项卡。因此,对于每个新选项卡,都应该添加Listview(此Listview对于所有选项卡都是通用的)。选项卡上的“X”标记也可以关闭选项卡。因此,Listview将包含1.工作簿,2。描述。。等等作为列。希望我有点道理。如果你需要,我可以解释。谢谢RammHi Dreas,我编辑了这个问题。1.基本问题:访问按钮的方式将保持不变,如果按钮位于inturn所在的内部??我的意思是浏览访问按钮最简单的方法是在按钮上设置名称。然后你可以用代码直接访问它。我认为我的答案现在已经完成了,基于我是如何理解你的问题的。你现在明白这些观念了吗?
            <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>
    
    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);
        }
    }