0

这看起来很简单,但我想不通!在这里寻求一点帮助。我有一堆 ListBoxItems,并希望它们在丢弃时转储不同的文本。

是否可以有一个基于下拉事件的 ListBoxItem 名称的 if 语句?

XAML:

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

代码背后:

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!");
        }

我应该将我的 listboxitems 转换为字符串,然后为每个 or 执行 if 语句吗?谢谢

4

3 回答 3

0

您应该能够在 DropImage 方法周围放置一个 IF 或 Case 语句。

我要做的是创建一个继承 ListBoxItem 的新 ListBoxItem 控件,并添加一个名为“Drop Text”的 Dependecy 属性。

然后,当您添加列表项时,设置 Drop Text String Dependency 属性,当您将项放到 Canvas 上时,您只需将 TextBox.Text 更改为 ListBoxItem 上的属性即可。

希望我在正确的轨道上与您想要的东西。

于 2011-04-21T20:36:48.957 回答
0

我不确定我的问题是否正确......但也许它会有所帮助。

<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>

和代码:

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);
    }
于 2011-04-22T08:49:58.703 回答
0

希望这个链接对你有帮助......

拖动并选择列表框项目?

于 2011-04-21T20:47:08.540 回答