【问题标题】:How can i get all the buttons in the uniformgrid and scroll it to see them all?我怎样才能得到uniformgrid中的所有按钮并滚动它来查看它们?
【发布时间】:2012-01-08 20:10:15
【问题描述】:

我正在构建一个应用程序来显示计算机中安装的所有软件,我已经有了所有按钮以显示相应的图标,但是当我显示它们时,uniformgrid 只显示适合窗口的按钮,我以为滚动条会显示它们,但我到了窗口的尽头,按钮仍然不见了!我怎样才能用滚动条显示它们? 这是 XAML 代码:

<Window x:Class="apple.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"> 
        <Grid>
            <DockPanel Name="dock">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <UniformGrid Name="gridx" DockPanel.Dock="Top" Rows="7" Columns="7">

                </UniformGrid>
            </ScrollViewer>
        </DockPanel>
    </Grid>
</Window>

这里是c#代码:

namespace apple
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string[] link = Directory.GetFiles(@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs", "*.lnk", SearchOption.AllDirectories);

        public MainWindow()
        {
            this.ResizeMode = ResizeMode.NoResize;
            //this.WindowStyle = WindowStyle.None;
            this.WindowState = WindowState.Maximized;
            InitializeComponent();
            masterGUI();
        }

        public void masterGUI()
        {
            gridx.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
            IconImage[] ico = null;
            Bitmap[] img = null;
            string[] list = null;
            list = new string[link.Length];
            ico = new Icon[link.Length];
            img = new Bitmap[link.Length];
            for (int n = 0; n < link.Length; n++)
            {
                ImageBrush ib = new ImageBrush();
                System.Windows.Controls.Button newBtn = new Button();
                list[n] = System.IO.Path.GetFileNameWithoutExtension(link[n]);
                FileToImageIconConverter some = new FileToImageIconConverter(link[n]);
                ImageSource imgSource = some.Icon;
                ib.ImageSource = imgSource;
                newBtn.Name = "a" + n;
                newBtn.Background = ib;
                newBtn.Content = list[n];
                newBtn.Click += new RoutedEventHandler(newBtn_Click);
                gridx.Children.Add(newBtn);
            }  
        }

        private void newBtn_Click(object sender, RoutedEventArgs e)
        {
            Button clicked = (Button)sender;
            string test = null;
            test = clicked.Name.Replace("a","0");
            this.Close();
            System.Diagnostics.Process.Start(link[Int32.Parse(test)]);
        }
    }
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    删除GridDockPanel 并设置UniformGrid.RowsUniformGrid.Columns,不能同时设置。您只需要WindowScrollViewerUniformGrid

    <Window>
        <ScrollViewer>
            <UniformGrid Name="gridx" Columns="7"/>
        </ScrollViewer>
    </Window>
    

    要以更惯用的 WPF 方式进行,你应该有这样的东西:

    <Window>
        <ScrollViewer>
            <ItemsControl ItemsSource="{Binding Programs}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="7"/>
    

    然后,您将从您的数据源公开一个Programs 集合,从而为每个已安装的程序自动生成一个项目。

    【讨论】:

    • 非常感谢,有没有办法控制按钮背景的大小?
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 2013-03-14
    • 2018-04-15
    • 1970-01-01
    • 2010-12-06
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多