2009-07-16 39 views
0

我有几个问题,因为我第一次使用XAML。如何使用C编程访问按钮如果按钮在里面使用C#

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

  2. 我可以使用我在下面显示的方式吗? 实际上第一个dockpanel会保存图像和一些标签,另一个dockpanel会在其中有tabcontrol。

  3. 如果我有一个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

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)我试图simplif y 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> 

您也可以在选项卡项目标题中有一个关闭按钮。由于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); 
    } 
} 
+0

嗨,我使用C#。 (该主题已列入C#:)) 我对此WPF和XAML很感兴趣,所以无法理清如何在此工作。 请帮忙 – Ramm 2009-07-16 10:44:55