简体   繁体   English

动态控制 WPF 中的按钮数量

[英]Dynamically control the number of buttons in WPF

I'm working with WPF recently.我最近正在使用 WPF。 Now, i'm facing a problem.现在,我面临一个问题。

I have a button "ADD", every time click on this will add a new row with some contents.我有一个“添加”按钮,每次单击它都会添加一个包含一些内容的新行。 Those contents are shown below-这些内容如下所示——

                    <TextBox Text="{Binding Name}" Margin="10,10" Height="20"  ></TextBox>
                    <TextBox Text="{Binding City}" Margin="10,10" Height="20" ></TextBox>
                    <TextBox Text="{Binding Age}" Margin="10,10" Height="20"></TextBox>
                    <TextBox Text="{Binding Count}" Margin="10,10" Height="20" ></TextBox>
                    <Button Content="M1" Margin="10,10" Height="20"/>
                    <Button Content="M2" Margin="10,10" Height="20"/>
                    <Button Content="M3" Margin="10,10" Height="20"/>
                </WrapPanel>
            </Grid>

Here, at the end there are three buttons M1,M2,M3.这里,最后有三个按钮 M1、M2、M3。 But, I don't need this all three buttons every time.但是,我不需要每次都需要这三个按钮。 I may need only M1 or only M2 or only M3 or M1,M2 etc.我可能只需要 M1 或只需要 M2 或只需要 M3 或 M1、M2 等。

How can I do this in c#?我怎样才能在 C# 中做到这一点? Actually i don't even know, am i in the right way?其实我什至不知道,我是在正确的方式?

Thanks in advance.提前致谢。

You could use DataBinding if the amount of buttons is dependent on the size of a list/collection.如果按钮的数量取决于列表/集合的大小,则可以使用 DataBinding。

<ItemsControl ItemsSource={Binding Ms}>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Name}" Command={Binding ThingToDoWhenClickedCommand}/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

This will generate exactly the amount of buttons that is in the Ms collection/List that is in the DataContext.这将准确生成 DataContext 中 Ms 集合/列表中的按钮数量。

I would highly recommend looking into the MVVM design pattern when working with WPF我强烈建议在使用 WPF 时研究 MVVM 设计模式

That said, I would bind my XAML to an ObservableCollection<SomeObject> , and clicking the AddButton would add a new SomeObject to the ObservableCollection .也就是说,我会将我的 XAML 绑定到ObservableCollection<SomeObject> ,然后单击AddButton会将新的SomeObject添加到ObservableCollection This would make the UI automatically add the new row when the collection gets updated, and SomeObject could have properties for IsM1Visible , IsM2Visible , and IsM3Visible which determines which buttons are visible.这将使 UI 在集合更新时自动添加新行,并且SomeObject可以具有IsM1VisibleIsM2VisibleIsM3Visible的属性,这些属性确定哪些按钮是可见的。

For example,例如,

Class SomeObject would have SomeObject类将具有

string Name;
string City;
int Age;
int Count;
bool IsM1Visible;
bool IsM2Visible;
bool IsM3Visible;

The XAML would look something like this: XAML 看起来像这样:

<ItemsControl ItemsSource="{Binding SomeCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBox Text="{Binding Name}" Margin="10,10" Height="20"  ></TextBox>
                <TextBox Text="{Binding City}" Margin="10,10" Height="20" ></TextBox>
                <TextBox Text="{Binding Age}" Margin="10,10" Height="20"></TextBox>
                <TextBox Text="{Binding Count}" Margin="10,10" Height="20" ></TextBox>
                <Button Content="M1" Visibility="{Binding IsM1Visible, Converter="{StaticResource BooleanToVisibilityConverter}" Margin="10,10" Height="20"/>
                <Button Content="M2" Visibility="{Binding IsM2Visible, Converter="{StaticResource BooleanToVisibilityConverter}" Margin="10,10" Height="20"/>
                <Button Content="M3" Visibility="{Binding IsM3Visible, Converter="{StaticResource BooleanToVisibilityConverter}" Margin="10,10" Height="20"/>
            </WrapPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

And the Add Button's Click event would look something like this: Add Button 的 Click 事件看起来像这样:

void AddButton_Click(object sender, EventArgs e)
{
    var newItem = new SomeItem
        {
            Name = "Something",
            City = "Something",
            Age = 30,
            Count = 2,
            IsM1Visible = true,
            IsM2Visible = false,
            IsM3Visible = true
        };

    SomeCollection.Add(newItem);
}

您可以通过代码隐藏文件添加按钮,并在按钮周围放置一个 if 结构来确定应该加载哪个按钮。

Are you using MVVM?你在使用 MVVM 吗?

If so then this is easy.如果是这样,那么这很容易。 Just create a ButtonViewModel object that represents one of your buttons and expose an ObservableCollection of them from your main ViewModel.只需创建一个代表您的按钮之一的 ButtonViewModel 对象,并从您的主 ViewModel 中公开它们的 ObservableCollection。

In your view just have a ListView, or just an ItemsControl bound to the collection, and a DataTemplate that turns your ButtonViewModel into a button.在您的视图中,只有一个 ListView,或者只是一个绑定到集合的 ItemsControl,以及一个将您的 ButtonViewModel 变成按钮的 DataTemplate。

When the user clicks the Add button, add a new ButtonViewModel to your collection and the view will update itself to match.当用户单击“添加”按钮时,将新的 ButtonViewModel 添加到您的集合中,视图将自行更新以匹配。

First, let create a view model for a row.首先,让我们为一行创建一个视图模型。 In that model you will have 3 bool properties, IsButton1Visible, IsButton2Visible, IsButton3Visible, or something like that with all properties you need to do binding on your row.在该模型中,您将拥有 3 个 bool 属性,IsButton1Visible、IsButton2Visible、IsButton3Visible 或类似的所有属性,您需要在行上进行绑定。 Second, your scenario is that when you click Add, new row will be added.其次,您的情况是,当您单击添加时,将添加新行。 So you have a list of Row_View_Model.所以你有一个 Row_View_Model 列表。 On AddCommand, you will at a new Row_View_Model into the list.在 AddCommand 上,您将在列表中添加一个新的 Row_View_Model。 Here you have full control on what button you want to show.在这里,您可以完全控制要显示的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM