簡體   English   中英

對話框窗口取決於viewmodel定義的按鈕數量

[英]Dialog window that depends on the number of buttons defined by viewmodel

我有類似的viewmodel:

public class MyDialogViewModel
{
    public MyButtonViewModel[] Buttons{ get; }
    public string Title { get; set; }
    public string Message { get; set; }

    public MyDialogViewModel(InputObject input)
    {
        Title= input.Title;
        Message = input.Message;
        Buttons= input.Buttons;
    }
}

public class MyButtonViewModel
{
    public MyButtonViewModel(ICommand command, string text)
    {
        Command = command;
        Text = text;
    }

    public ICommand Command { get; }
    public string Text { get; }
}

我無法更改此視圖模型。

現在,我需要使用MyButtonViewModel[]表示的自定義按鈕組來制作充當對話框窗口的窗口。 MyButtonViewModel[]可以具有不同數量的對象。

我該如何制作窗口,使其具有適合MyButtonViewModel[]大小的大小,並在每個呈現的MyButtonViewModel綁定一組按鈕?

為了清楚MyButtonViewModel[] -如果MyButtonViewModel[]具有2個元素-窗口應具有2個按鈕。 用於3個元素的3個按鈕,依此類推。

使用ItemsControl

<ItemsControl ItemsSource="{Binding Buttons}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

您還可以設置SizeToContent="WidthAndHeight"以便自動適應窗口。

使用@Babbillumpa建議的ItemsControl 它將在ItemsSource為每個項目創建一個元素。

但是,您還需要定義如何在屏幕上表示MyButtonViewModel 您可以通過定義ItemTemplate 在這里,您可以創建綁定到TextCommand源屬性的實際Button元素:

<ItemsControl ItemsSource="{Binding Buttons, Mode=OneTime}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding Text, Mode=OneTime}" 
                    Command="{Binding Command, Mode=OneTime}"
                    Margin="0,0,5,0"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM