简体   繁体   English

WPF C#ListBoxItem代码背后的if语句

[英]WPF C# ListBoxItem Code Behind for If Statement

This seems simple but I can't figure it out! 这似乎很简单,但我无法弄清楚! Looking for a little help here. 在这里寻找一点帮助。 I have a bunch of ListBoxItems and would like them to dump different text out when dropped. 我有一堆ListBoxItems,希望它们在放下时转储不同的文本。

Is it possible to have an if statement based on the ListBoxItem name for the drop event? 是否有可能基于基于dropBox的ListBoxItem名称的if语句?

XAML: XAML:

<ListBoxItem x:Name="ActionItem">
<Image Source="Action.png" Height="60" Width="60" ToolTip="Action"/>

Code Behind: 背后的代码:

private void DropImage(object sender, System.Windows.DragEventArgs e)
    {
                    {

            ImageSource image = e.Data.GetData(typeof(ImageSource)) as ImageSource;

            Image imageControl = new Image() { Width = image.Width, Height = image.Height, Source = image };

            Canvas.SetLeft(imageControl, e.GetPosition(this.MyCanvas).X);

            Canvas.SetTop(imageControl, e.GetPosition(this.MyCanvas).Y);

            this.Canvas.Children.Add(imageControl);

            TextBox.Text = ("This is a test!");
        }

Should I be converting my listboxitems so strings and then doing an if statement for each or? 我应该将listboxitems转换为字符串,然后对每个or进行if语句吗? THanks 谢谢

You should be able to place a IF or Case Statement around the DropImage method. 您应该能够在DropImage方法周围放置IF或Case语句。

What i would do is i would create a new ListBoxItem control that inherits ListBoxItem, and add a Dependecy Property called "Drop Text". 我要做的是创建一个继承ListBoxItem的新ListBoxItem控件,并添加一个名为“ Drop Text”的Dependecy属性。

Then when you add the List Items set the Drop Text String Dependency Property, and when you drop the item on to the Canvas you can simply just change TextBox.Text to the Property on the ListBoxItem. 然后,当您添加列表项时,请设置“放置文本字符串依赖项”属性,然后将其拖放到画布上,只需将TextBox.Text更改为ListBoxItem的属性即可。

Hope i am on the right track with what you are wanting. 希望我能按照您的要求走上正轨。

Hope this links will help you..... 希望此链接可以帮助您.....

Drag and Select ListBox Items? 拖动并选择ListBox项目?

I'm not sure i've hot the problem right...But maybe it helps. 我不确定我是否已经解决了这个问题...但是也许有帮助。

<StackPanel>
        <ListBox AllowDrop="True">
            <ListBoxItem x:Name="ActionItem" PreviewMouseDown="ActionItem_PreviewMouseDown">
                <Image Source="links.png" Height="60" Width="60" ToolTip="Action" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=Name}"/>
            </ListBoxItem>
        </ListBox>

        <Canvas Height="100" AllowDrop="True" Name="MyCanvas" Drop="Canvas_Drop" Background="Azure"/>
        <TextBox Name="MyTextBox">Text</TextBox>
    </StackPanel>

and code: 和代码:

private void Canvas_Drop(object sender, DragEventArgs e)
    {
        Image image = e.Data.GetData(typeof(Image)) as Image;
        ListBoxItem lbi = image.Parent as ListBoxItem;
        Image imageControl = new Image() { Width = image.Width, Height = image.Height, Source = image.Source };

        Canvas.SetLeft(imageControl, e.GetPosition(this.MyCanvas).X);
        Canvas.SetTop(imageControl, e.GetPosition(this.MyCanvas).Y);
        this.MyCanvas.Children.Add(imageControl);

        MyTextBox.Text = string.Format("ListBoxItem name is: through item {0}, through image tag: {1}", lbi.Name, image.Tag);
    }

    private void ActionItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        ListBoxItem lbi = (ListBoxItem)sender;
        DragDrop.DoDragDrop(lbi, lbi.Content, DragDropEffects.Copy);
    }

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

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