绑定 ListBox 项目索引

huangapple go评论69阅读模式
英文:

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"

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

Can you help me?

答案1

得分: 1

你可以将 SelectedLockView 绑定到你的 ListBox 的 SelectedItem

&lt;ToolBar Style=&quot;{DynamicResource MaterialDesignToolBar}&quot; HorizontalAlignment=&quot;Left&quot;&gt;
	&lt;ListBox Height=&quot;50&quot; x:Name=&quot;ListBoxLockView&quot; VerticalAlignment=&quot;center&quot; SelectedItem=&quot;{Binding SelectedLockView}&quot;&gt;
	   &lt;ListBoxItem x:Name=&quot;Lock&quot; ToolTip=&quot;V&#233;rouillage des vues&quot;&gt;
		   &lt;materialDesign:PackIcon Kind=&quot;lock&quot;/&gt;
	   &lt;/ListBoxItem&gt;
	   &lt;ListBoxItem x:Name=&quot;UnLock&quot; ToolTip=&quot;D&#233;v&#233;rouillage des vues&quot;&gt;
		   &lt;materialDesign:PackIcon Kind=&quot;UnlockedMinus&quot;  /&gt;
	   &lt;/ListBoxItem&gt;
   &lt;/ListBox&gt;
   &lt;TextBlock Text=&quot;{Binding MsgVues}&quot; /&gt;
&lt;/ToolBar&gt;

当你在 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 &amp; &quot; was selected.&quot;)
    End Set
End Property
英文:

You can bind SelectedLockView to your ListBox's SelectedItem:

&lt;ToolBar Style=&quot;{DynamicResource MaterialDesignToolBar}&quot; HorizontalAlignment=&quot;Left&quot;&gt;
	&lt;ListBox Height=&quot;50&quot; x:Name=&quot;ListBoxLockView&quot; VerticalAlignment=&quot;center&quot; SelectedItem=&quot;{Binding SelectedLockView}&quot;&gt;
	   &lt;ListBoxItem x:Name=&quot;Lock&quot; ToolTip=&quot;V&#233;rouillage des vues&quot;&gt;
		   &lt;materialDesign:PackIcon Kind=&quot;lock&quot;/&gt;
	   &lt;/ListBoxItem&gt;
	   &lt;ListBoxItem x:Name=&quot;UnLock&quot; ToolTip=&quot;D&#233;v&#233;rouillage des vues&quot;&gt;
		   &lt;materialDesign:PackIcon Kind=&quot;UnlockedMinus&quot;  /&gt;
	   &lt;/ListBoxItem&gt;
   &lt;/ListBox&gt;
   &lt;TextBlock Text=&quot;{Binding MsgVues}&quot; /&gt;
&lt;/ToolBar&gt;

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 &amp; &quot; was selected.&quot;)
    End Set
End Property

huangapple
  • 本文由 发表于 2023年6月29日 21:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定