简体   繁体   English

如何从选定的ListBox控件的listboxitem获取属性? C#

[英]How do i get a property from a selected ListBox Control 's listboxitem ? C#

like the title said i want to get the value of a property from a selected listboxitem on a button click 就像标题说的那样,我想从按钮上单击的选定列表框项中获取属性的值

 <ListBox x:Name="IconListbox" Background="{x:Null}" BorderBrush="Black">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBoxItem x:Name="defaultIcon">
            <Grid Background="Black">
                <Border BorderBrush="#FF1EF3F3" BorderThickness="2">
                    <Image x:Name="defaultIconImage" Width="50" Height="50" Source="icon.png"/>
                </Border>
            </Grid>
        </ListBoxItem>
        <ListBoxItem>
            <Grid Background="Black">
                <Border BorderBrush="#FF1EF3F3" BorderThickness="2">
                    <Image x:Name="secondIconImage" Width="50" Height="50" Source="SecondIcon.png"/>
                </Border>
            </Grid>
        </ListBoxItem>
    </ListBox>

For example if i click the button it should return the image source of the current selected item. 例如,如果我单击按钮,它将返回当前所选项目的图像源。 So if ListboxItem defaultIcon is selected it should return defaulticon.png. 因此,如果选择了ListboxItem defaultIcon,则应返回defaulticon.png。 How can i do this ? 我怎样才能做到这一点 ?

Edit: 编辑:

Maybe i am taking the wrong aproach by trying to use a listbox. 也许我通过尝试使用列表框而采取了错误的方法。 I'm verry new to Xaml code and i'll try to better explain what i want as a result. 我对Xaml代码非常陌生,因此我将尽力更好地解释我想要的结果。

Here is a picture that i will use to try and explain: Image 这是一张图片,我将用来尝试解释: 图片

So what i want is when 1 is selected i need it to return the source of the blue flame image when i click the save button 所以我想要的是当选择1时,当我单击“保存”按钮时,我需要它来返回蓝色火焰图像的来源

when 2 is selected i need it to return the source of the blue facebook image when i click the save button 当选择2时,我单击保存按钮时需要它返回蓝色Facebook图像的来源

IconListbox.SelectedItem and then cast it or create a new object of them. IconListbox.SelectedItem,然后对其进行投射或创建它们的新对象。

Example

Image i = (Image)IconListbox.SelectedItem;

You can find the Image inside SelectedItem like the code below 您可以在SelectedItem内找到图像,如下面的代码

        var selectedItem = IconListbox.SelectedItem as ListBoxItem;
        if (selectedItem != null)
        {
            var image = selectedItem.GetChildOfType<Image>();
            if (image != null)
            {
                var source = image.Source;
            }
        }

extension method to get child of specific type 扩展方法以获取特定类型的子级

     public static T GetChildOfType<T>(this DependencyObject depObj) where T : DependencyObject
        {
            if (depObj == null) return null;

            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                var child = VisualTreeHelper.GetChild(depObj, i);

                var result = (child as T) ?? GetChildOfType<T>(child);
                if (result != null) return result;
            }
            return null;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM