【问题标题】:How do I retrieve text from a textblock inside a listbox and display the text in a textbox?如何从列表框中的文本块中检索文本并在文本框中显示文本?
【发布时间】:2013-07-06 09:46:01
【问题描述】:

如何从列表框内的文本块中检索文本并在文本框中显示文本?

我想做的事

首先我希望能够从列表框内的文本块中复制文本

那我想在文本框中显示文字

我尝试使用可视化树助手,但显然它找不到“FindName”方法。有没有更好的方法来实现这一点?

XAML 代码

 <ListBox Name="ChatDialogBox" Height="550" ItemsSource="{Binding Path=Instance.Messages,Source={StaticResource Binder}}" > 
            <ListBox.ItemTemplate>  
                <DataTemplate>                   
                            <Grid>                       
                                <TextBlock Name="ChatMessage" Text="{Binding Text}" TextWrapping="Wrap" Width="430">         
                     <toolkit:ContextMenuService.ContextMenu>
                        <toolkit:ContextMenu Name="ContextMenu" >
                              <toolkit:MenuItem Name="Copy"  Header="Copy" Click="Copy_Click" />
                        </toolkit:ContextMenu>
                    </toolkit:ContextMenuService.ContextMenu>          
                                </TextBlock>                             
                            </Grid>                 
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

代码背后

 private void Copy_Click(object sender, RoutedEventArgs e)
    {

        ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(ChatDialogBox);
        DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
        TextBlock target = (TextBlock)myDataTemplate.FindName("ChatMessage", myContentPresenter);

    }


    private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is childItem)
                return (childItem)child;
            else
            {
                childItem childOfChild = FindVisualChild<childItem>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }

活页夹类

 public class Binder : INotifyPropertyChanged
{
    static Binder instance = null;
    static readonly object padlock = new object();

    public Binder()
    {
        Messages = new ObservableCollection<Message>();
    }

    public static Binder Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Binder();
                }
                return instance;
            }
        }
    }

    private ObservableCollection<Message> messages;
    public ObservableCollection<Message> Messages
    {
        get
        {
            return messages;
        }
        set
        {
            if (messages != value)
            {
                messages = value;
                NotifyPropertyChanged("Messages");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => { PropertyChanged(this, new PropertyChangedEventArgs(info)); });
        }
    }
}

消息类

 public class Message 
{
    public string Text { get; set; }

}

【问题讨论】:

    标签: c# listbox windows-phone-8 silverlight-toolkit visualtreehelper


    【解决方案1】:

    有一种间接但更简单的方法来检索文本块的内容。

    在点击事件中,您可以使用DataContext 属性检索模型中的对象:

    private void Copy_Click(object sender, RoutedEventArgs e)
    {
        var model = (Message)((FrameworkElement)sender).DataContext;
    
        // Display model.Text in your TextBlock
    }
    

    只需将 Message 替换为您分配给列表框 ItemsSource 的对象类型即可。

    【讨论】:

    • 我试过这个 var model = (Binder.Instance.Messages ((FrameworkElement)sender).DataContext; 但我收到一条错误消息,说“是一个属性,但像一个类型一样使用”那么我该怎么做当我的活页夹是可观察的集合时开始分配
    • Binder.Instance.Messages的类型是什么?例如,如果是ObservableCollection&lt;Message&gt;,那么你需要写(Message)((FrameworkElement)sender).DataContext
    • 是的,它是 ObservableCollection。但问题是如果我把(Message)((FrameworkElement)sender).DataContex,我会在文本框中得到myapp.Message
    • @Challenger 好吧,一旦您检索到消息,只需使用message.Text
    • @Challenger Everything :D 使用 .ToString 您试图将对象转换为字符串,这在默认情况下是不可能的。使用 .Text,您正在尝试检索 Text 属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多