0

タイトルの言い方がよくわからず、関連する質問が見つかりませんでしたが、もしあれば教えてください。

表示されるボタンの数が、ユーザーが設定した入札の種類 (現金、小切手、クレジット、デビット、ギフトカードなど) によって決まる入札画面を作成しようとしています。

したがって、次のようなクラスがあるとします。

public class TenderType
{
    public string DisplayName { get; set; }

    // ... other implementation details
}

そして、私の DataContext には、次のように宣言された TenderTypes コレクションがあります。

public ObservableCollection<TenderType> TenderTypes { get; private set; }

次に、コレクション内の TenderType インスタンスの数に応じて表示されるボタンの数をビューに更新し、それらの Text プロパティをコレクション内の適切な項目の DisplayName にバインドするにはどうすればよいでしょうか?

4

1 に答える 1

1

ItemsControlを使用して、TenderTypeのデータテンプレートを作成してボタンを表示できます。

このようにすると、リスト内のボタンのみが表示されます

Xaml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication8"
        Title="MainWindow" Height="105" Width="156" Name="UI">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:TenderType}">
            <Button Content="{Binding DisplayName}" />
        </DataTemplate>
    </Window.Resources>

    <Grid DataContext="{Binding ElementName=UI}">
        <ItemsControl ItemsSource="{Binding TenderTypes}"/>
    </Grid>
</Window>

コード:

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

    public MainWindow()
    {
        InitializeComponent();
        TenderTypes.Add(new TenderType { DisplayName = "Stack" });
        TenderTypes.Add(new TenderType { DisplayName = "Overflow" });
    }

    public ObservableCollection<TenderType> TenderTypes 
    {
        get { return _sourceData; }
        set { _sourceData = value; }
    }
}

public class TenderType
{
    public string DisplayName { get; set; }
}

結果:

ここに画像の説明を入力してください

于 2013-01-21T22:49:43.790 に答える