【问题标题】:Loading Buttons on window loaded event depending on Number of User [closed]根据用户数量在窗口加载事件上加载按钮[关闭]
【发布时间】:2013-01-29 22:38:18
【问题描述】:

我们有 WPF 应用程序,我们想要如下功能。 我想要一个表单,其中将在每个用户前面包含 用户名一个按钮。 意思是如果数据库有 10 个用户,当窗口加载时它会带来这 10 个用户,并且在每个用户的前面会有一个显示按钮。

【问题讨论】:

  • WPF 可以做到这一切,祝你好运
  • 但是如何,请帮助我。
  • 您需要帮助哪一部分,整个事情??只需将用户放在ListBox 中,然后使用LabelButton 创建一个DataTemplate
  • 不,但是我需要使用哪个控件,这意味着很容易,我有没有用户,但是如何在他们面前显示按钮?
  • 好的,但是它会加载显示按钮吗?

标签: wpf wpf-controls wpfdatagrid wpftoolkit


【解决方案1】:

您可以使用ListBox 并创建一个包含您的LabelButtonDataTemplate

例子:

Xaml:

<Window x:Class="WpfApplication16.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication16"
        Title="MainWindow" Height="350" Width="525" Name="UI">
    <Window.Resources>

        <!-- DataTemplate for User object -->
        <DataTemplate DataType="{x:Type local:User}" >
            <Border CornerRadius="3" BorderBrush="Black" BorderThickness="1" Margin="2" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding UserName}" Width="100"/>
                    <Button Content="Show User" Width="100" Margin="2"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>

    <Grid DataContext="{Binding ElementName=UI}">
        <!-- The ListBox -->
        <ListBox ItemsSource="{Binding Users}" />
    </Grid>
</Window>

代码:

public partial class MainWindow : Window
{
    private ObservableCollection<User> _users = new ObservableCollection<User>();

    public MainWindow()
    {
        InitializeComponent();

        // Load users
        for (int i = 0; i < 10; i++)
        {
            Users.Add(new User { UserName = "User " + i });
        }
    }

    public ObservableCollection<User> Users
    {
        get { return _users; }
        set { _users = value; }
    }
}

public class User
{
    public string UserName { get; set; }
}

结果

【讨论】:

  • 非常感谢您的帮助,非常感谢。
猜你喜欢
  • 2018-07-18
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多