英文:
Binding ListBox Item Index
问题
I have a ListBox in a ToolBar with images.
I want to bind the selected item with a property "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="锁定视图">
<materialDesign:PackIcon Kind="lock"/>
</ListBoxItem>
<ListBoxItem x:Name="UnLock" ToolTip="解锁视图">
<materialDesign:PackIcon Kind="UnlockedMinus" />
</ListBoxItem>
</ListBox>
<TextBlock Text="{Binding MsgVues}" />
</ToolBar>
Can you help me?
英文:
I have a ListBox in a ToolBAr with images.
I want to bind the selected item with a property "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>
Can you help me?
答案1
得分: 1
你可以将 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
属性的 setter 代码中,你将得到一个 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
英文:
You can bind SelectedLockView
to your ListBox's 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>
When you select an item in the ListBox, the binding will receive the newly selected item. In the SelectedLockView
property setter code, you will get a ListBoxItem as the value
. SelectedLockView
should therefore be an Object or a ListBoxItem. If it is an Object, you can use CType(value, ListBoxItem)
to convert it to a ListBoxItem. You can then get the name of the selected item:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论