0
<ListBox Name="serumListBox" VerticalContentAlignment="Stretch" 
    ScrollViewer.HorizontalScrollBarVisibility="Visible" ItemsSource="{Binding Path=SerumList}">
        <ListBox.Resources>
            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" 
                        Value="True">
                        <Setter Property="IsSelected" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical" >
                    <Button Width="250" Height="70"
                        HorizontalContentAlignment="Stretch" Click="SerumListItem_Click" >
                        <Button.ContentTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <Button Click="SerumListItemRemove_Click" VerticalAlignment="Top" HorizontalAlignment="Right" Width="22" >
                                        <Image Source="./Images/close.png"></Image>
                                    </Button>
                                    <Label VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,-9,0,0" Content="{Binding Name}" />
                                    <Label VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,-4,0,0" Content="{Binding LotNum}"/>
                                </StackPanel>
                            </DataTemplate>
                        </Button.ContentTemplate>
                    </Button>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

コードビハインド:

public partial class MainWindow : MetroWindow
{
    private ObservableCollection<Serum> serumList = new ObservableCollection<Serum>();

    public ObservableCollection<Serum> SerumList 
    {
        get { return serumList; }
        set { serumList = value; }
    }

    public MainWindow()
    {
        InitializeComponent();
        serumListBox.ItemsSource = SerumList;
    }

    private void buttonInsert_Click(object sender, RoutedEventArgs e)
    {
        serumList.Add(new Serum() { Name = "aadasfas", Year = "2", Month = "2", LotNum = "2", Type = "2" });
    }

    private void SerumListItemRemove_Click(object sender, RoutedEventArgs e)
    {
        serumList.RemoveAt(serumListBox.SelectedIndex);
    }

    private void SerumListItem_Click(object sender, RoutedEventArgs e)
    {

    }
}

ボタン データ テンプレートはリストボックス データテンプレートのネストされたテンプレートであるため、ラベル コンテンツを適切にバインドできません。この種のリソースや例も見つかりませんでした。何かアイデアはありますか?

4

1 に答える 1

1

を次のように試してくださいLabel.Content Binding

...
<Label Margin="0,-9,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Content="{Binding DataContext.Name,
                          RelativeSource={RelativeSource FindAncestor,
                                                        AncestorType={x:Type ListBoxItem}}}" />
<Label Margin="0,-4,0,0"
        HorizontalAlignment="Left"
        VerticalAlignment="Top"
        Content="{Binding DataContext.LotNum,
                          RelativeSource={RelativeSource FindAncestor,
                                                        AncestorType={x:Type ListBoxItem}}}" />
...
于 2013-06-29T12:07:32.253 に答える