XAML 绑定列表框项索引

9w11ddsr  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(119)

我有一个列表框中的一个ToolBAr与图像。我想用属性“SelectedLockView”绑定所选项目

<ToolBar   Style="{DynamicResource MaterialDesignToolBar}"   HorizontalAlignment="Left" >
        <ListBox Height="50"  x:Name="ListBoxLockView"  VerticalAlignment="center">
            <Behaviors:Interaction.Triggers>
                <Behaviors:EventTrigger EventName="SelectionChanged">
                    <Behaviors:InvokeCommandAction Command="{Binding ListBoxLockView_SelectionChanged}"/>
                </Behaviors:EventTrigger>
           </Behaviors:Interaction.Triggers>
           <ListBoxItem x:Name="Lock" ToolTip="Vérouillage des vues">
               <materialDesign:PackIcon Kind="lock"/>
           </ListBoxItem>
           <ListBoxItem x:Name="UnLock" ToolTip="Dévérouillage des vues">
               <materialDesign:PackIcon Kind="UnlockedMinus"  />
           </ListBoxItem>
       </ListBox>
       <TextBlock Text="{Binding MsgVues}" />
    </ToolBar>

字符串
你能帮我吗?

mo49yndu

mo49yndu1#

您可以将SelectedLockView绑定到ListBox的SelectedItem

<ToolBar Style="{DynamicResource MaterialDesignToolBar}" HorizontalAlignment="Left">
    <ListBox Height="50" x:Name="ListBoxLockView" VerticalAlignment="center" SelectedItem="{Binding SelectedLockView}">
       <ListBoxItem x:Name="Lock" ToolTip="Vérouillage des vues">
           <materialDesign:PackIcon Kind="lock"/>
       </ListBoxItem>
       <ListBoxItem x:Name="UnLock" ToolTip="Dévérouillage des vues">
           <materialDesign:PackIcon Kind="UnlockedMinus"  />
       </ListBoxItem>
   </ListBox>
   <TextBlock Text="{Binding MsgVues}" />
</ToolBar>

字符串
当您在ListBox中选取项目时,系结将接收新选取的项目。在SelectedLockView属性设置器代码中,您将获得一个ListBoxItem作为value。因此SelectedLockView应该是Object或ListBoxItem。如果它是一个Object,可以使用CType(value, ListBoxItem)将其转换为ListBoxItem。然后,您可以获取所选项目的名称:

Private _selectedItem As ListBoxItem

Public Property SelectedLockView As ListBoxItem
    Get
        Return _selectedItem
    End Get
    Set(value As ListBoxItem)
        _selectedItem = value
        MsgBox(_selectedItem.Name & " was selected.")
    End Set
End Property

相关问题