开发者

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.

Is it possible to have an if statement based on the ListBoxItem name for the drop event?

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 开发者_Python百科I be converting my listboxitems so strings and then doing an if statement for each or? THanks


You should be able to place a IF or Case Statement around the DropImage method.

What i would do is i would create a new ListBoxItem control that inherits ListBoxItem, and add a Dependecy Property called "Drop Text".

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.

Hope i am on the right track with what you are wanting.


Hope this links will help you.....

Drag and Select ListBox Items?


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜