ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [WPF]ListBox 내에서 Button Command Binding
    C#/WPF 2022. 9. 5. 16:09

     

    <ListBox x:Name="deviceListBox"
             Margin="5"
             Foreground="AliceBlue"
                  MinWidth="250" MinHeight="180"
                 Grid.IsSharedSizeScope="True"
                 SelectedIndex="{Binding SelectedItemIndex}"
             ItemsSource="{Binding UDAs}">
            <ListBox.Style>
                <Style TargetType="ListBox">
                    <Setter Property="Background" Value="#253E60"/>
                    <Setter Property="ItemTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=SerialNumber}"
                                           Margin="0,2,0,0"/>
                                    <Button Content="download" 
                                            Margin="50,0,0,0"
                                            BorderThickness="0"
                                            Command="{Binding DataContext.SelDownload,RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}}">
                                        <Button.Style>
                                            <Style TargetType="Button">
                                                <Setter Property="Background" Value="#00B9CE"/>
                                                <Style.Triggers>
                                                    <Trigger Property="IsMouseOver" Value="True">
                                                        <Setter Property="Background" Value="#00DFF7"/>
                                                    </Trigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Button.Style>
                                        <Button.Template>
                                            <ControlTemplate TargetType="Button">
                                                <Border Width="100" Height="20"
                                                        CornerRadius="5"
                                                        Background="{TemplateBinding Background}">
                                                    <ContentPresenter VerticalAlignment="Center"
                                                        HorizontalAlignment="Center"/>
                                                </Border>
                                            </ControlTemplate>
                                        </Button.Template>
    
                                    </Button>
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.Style>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Padding" Value="3"/>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.Template>
                <ControlTemplate TargetType="ListBox">
                    <Border 
                        Width="{Binding ListBoxWidth}" Height="{Binding ListBoxHeight}"
                        CornerRadius="10"
                        Background="{TemplateBinding Background}">
                        <ItemsPresenter HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </ListBox.Template>
            </ListBox>

     

     

    Command="{Binding DataContext.SelDownload,

    RelativeSource={RelativeSource AncestorType=UserControl, AncestorLevel=1}}"

     


    ListBox 내 DataTemplate의 Button의 인덱스 지정 및 가져오기

    LitBox에 추가될 아이템의 Index저장 (SerialIndex)

     

    CommandParameter로 ItemsSource의 바인딩 모델의 SerialIndex 바인딩

     

    foreach문에서 값, index 얻기

     

    728x90

    'C# > WPF' 카테고리의 다른 글

    [WPF] ListBox ControlTemplate Scrollbar  (0) 2022.09.06
    [WPF] Grid, StackPanel, DockPanel  (0) 2022.09.06
    [WPF] Image source (Binding)  (0) 2022.09.05
    [WPF]MVVM  (0) 2022.07.11
    [WPF] Data Binding  (0) 2022.07.06

    댓글

Designed by Tistory.