在网格中显示列表框

时间:2011-10-16 08:02:07

标签: c# wpf listbox grid stackpanel

这可能是一个愚蠢的问题,但我坚持这样做:(。我有一个网格,有3列。我在这3列中的每一列都有一个文本框和一个列表框,如下所示:

<Grid.ColumnDefinitions>
                <ColumnDefinition Width="130"></ColumnDefinition>
                <ColumnDefinition Width="380"></ColumnDefinition>
                <ColumnDefinition Width="146"></ColumnDefinition>
             </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel Grid.Column="0" Grid.Row="0">
                <TextBox Text="File Name" Height="30"></TextBox>
            </StackPanel>
            <StackPanel Grid.Column="1" Grid.Row="0">
                <TextBox Text="File Path" Height="30"></TextBox>
            </StackPanel>
            <StackPanel Grid.Column="2" Grid.Row="0">
                <TextBox Text="File Size" Height="30"></TextBox>
            </StackPanel>

            <StackPanel Grid.Column="0">
                <ListBox Name="listbox_name" Margin="1,30" Height="276" />
             </StackPanel>
            <StackPanel Grid.Column="1">
                <ListBox Name="listbox_path" Margin="1,30" Height="276" />
            </StackPanel>
            <StackPanel Grid.Column="2">
                <ListBox Name="listbox_size" Margin="1,30" Height="276" />
            </StackPanel>

及其背后的代码:

public Window1()
        {
        InitializeComponent();

        list.Add("D:\\a\\hy");
        list.Add("D:\\a\\hy1");
        list.Sort();           
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        listbox_name.ItemsSource = list;
        grid1.Visibility = Visibility.Hidden;
    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {
        grid1.Visibility = Visibility.Visible;
    }

但是点击按钮,我无法看到列表框,显示列表。请指导我哪里出错了。谢谢!

2 个答案:

答案 0 :(得分:2)

原因是您的stackPanel位于Grid.Col="0"且非常小。但ListBox位于stackPanel内。它有一个保证金,而且会下降。您是否看不到自己的listBox


enter image description here
如果你会这样做:

<StackPanel Margin="0,0,0,-279">
            <ListBox Name="listbox_name" Margin="1,30" Height="276" />
        </StackPanel>

你会看到你的listBox,它会起作用。
注意:此代码仅为示例。您需要为窗口制作更好的布局。



以下是我如何制作一个窗口:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="678" Loaded="Window_Loaded">
    <Grid Name="grid1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="130"></ColumnDefinition>
            <ColumnDefinition Width="380"></ColumnDefinition>
            <ColumnDefinition Width="146"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>

        <TextBlock Text="File Name"  Grid.Row="0" Grid.Column="0" Margin="5" />
        <TextBlock Text="File Path"  Grid.Row="0" Grid.Column="1" Margin="5" />
        <TextBlock Text="File Size" Grid.Row="0" Grid.Column="2" Margin="5" />

        <ListBox Name="listbox_name" Grid.Row="1" Grid.Column="0" BorderBrush="Black" />
        <ListBox Name="listbox_path" Grid.Row="1" Grid.Column="1" BorderBrush="Black" />
        <ListBox Name="listbox_size" Grid.Row="1" Grid.Column="2" BorderBrush="Black" />
    </Grid>
</Window>

enter image description here

答案 1 :(得分:2)

    <ListView Name="list"
              Grid.Row="1" 
              ItemsSource={Binding Path=Files}                
              >
    <ListView.View>        
     <GridView>
        <GridViewColumn Header="Name">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button>
                     <TextBlock Text="{BindingPath=Name}"/>                            
                    </Button>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
        <GridViewColumn Header="Size">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button>
                     <TextBlock Text="{BindingPath=Size}"/>                            
                    </Button>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
        <GridViewColumn Header="Path">
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Button>
                     <TextBlock Text="{BindingPath=Path}"/>                            
                    </Button>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>
     </ListView.View>  
    </ListView>

我的ViewModel将是

public class FileListViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// 
    /// </summary>
    public ObservableCollection<Fileinfo> Files{ get; set; }

    private Fileinfo selectedFile;

    public Fileinfo SelectedFile
    {
        get { return selectedFile; }
        set
        {
            selectedFile= value;
            InvokePropertyChanged(new PropertyChangedEventArgs("SelectedFile"));
        }
    }

    public PersonListViewModel()
    {
                //Loading List
                   Files= new ObservableCollection<Fileinfo>()
              {    //This is temp list you modify accordinh to you logic
                                   new FileInfo{Name = "File32"},
                                   new FileInfo{Name = "File33"},
                                   new FileInfo{Name = "File373"},
                                   new FileInfo{Name = "File393"},
                                   new FileInfo{Name = "File345"},
                                   new FileInfo{Name = "File375"},
                                   new FileInfo{Name = "File395"},
                                   new FileInfo{Name = "File387"},
                                   new FileInfo{Name = "File387"}
                               };
    }

    #region Implementation of INotifyPropertyChanged

    /// <summary>
    /// 
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="e"></param>
    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, e);
    }

    #endregion
}

您的模型应该像

public class FileinFo: INotifyPropertyChanged
{
    private string size;
    public event PropertyChangedEventHandler PropertyChanged;
    private string name;
    private bool isSelected;

    public string Size
    {
        get { return size; }
        set
        {
            size= value;
            InvokePropertyChanged(new PropertyChangedEventArgs("size"));
        }
    }

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("Name"));
        }
    }



    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("IsSelected"));
        }
    }

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }
}  

您需要将yor的datacontext设置为ViewModel

所以假设您的视图命名为FileListView.xaml

然后在构造函数后面的代码中,您可以编写

this.DataContext= new FileListViewModel();