【问题标题】:How to select an item in a ListBox and pick the item's name from a DataTemplate in WPF如何选择 ListBox 中的项目并从 WPF 中的 DataTemplate 中选择项目的名称
【发布时间】:2014-10-19 18:53:44
【问题描述】:

我正在 WPF 中实现一个下载 UI,其中正在下载的每个文件都将显示在 DataTemplate 的列表框中

<ListBox>
   <ListBox.ItemTemplate>
      <DataTemplate> 
         <TextBlock x:Name="FileName" text={Binding FileName}" />
         <ProgressBar ... />
         <Button Content="Cancel" click="ButtonCancel_Click" />
      </DataTemplate>
   </ListBox.ItemTemplate>
<ListBox>

现在这个列表已经完美地填充了所有下载信息。我遇到的唯一问题是,当用户单击Cancel 按钮时,要取消下载,我必须从ObservableCollections 中删除一个条目。但是我在点击事件中没有File Name(我知道点击事件不是MVVM,我仍然想在点击事件处理程序中这样做)。

selectedItem 被取消时,任何人都可以建议我如何获得该特定文件的FileName。在

private void ButtonCancel_Click(...) {}

【问题讨论】:

  • 我不明白你的描述,也不明白这个问题。请更正描述以更准确。
  • 只是出于好奇,如果您的应用程序使用 MVVM 实现,为什么要在代码隐藏、按钮单击事件处理程序中执行此操作?
  • 好的,告诉我如何执行命令绑定...实际上它是一个 POC,而不是一个完整的开发人员,这就是原因

标签: c# .net wpf mvvm


【解决方案1】:

虽然我仍然鼓励您使用MVVM way of dealing with UI events,但您可以使用Cancel 按钮的点击事件处理程序来实现您想要的。

首先在您的 xaml 中,bind 文件名到 Cancel 按钮的 Tag 属性。

<ListBox>
   <ListBox.ItemTemplate>
      <DataTemplate> 
         <TextBlock x:Name="FileName" text={Binding FileName}" />
         <ProgressBar ... />
         <Button Content="Cancel" Tag="{Binding FileName}" 
                 Click="ButtonCancel_Click" />
      </DataTemplate>
   </ListBox.ItemTemplate>
<ListBox>

然后在您的点击事件处理程序中

private void ButtonCancel_Click(object sender, RoutedEventArgs e) 
{
   Button myButton = (Button)sender;
   string fileName = myButton.Tag.ToString();
   // use fileName
}

编辑

只是添加一个完整的示例,该示例已在本地测试并确保其有效。

XAML

<Window x:Class="WpfTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Name="listBox1">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock x:Name="FileName" Text="{Binding Path=FileName}" />

                         <Button Content="Cancel" Tag="{Binding Path=FileName}" 
                                 Click="ButtonCancel_Click" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

代码隐藏

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var fileNames = new List<DownloadModel>
            {
                new DownloadModel
                {
                    FileName = "File1"
                }, 
                new DownloadModel
                {
                    FileName = "File2"
                }, 
                new DownloadModel
                {
                    FileName = "File3"
                }
            };

            listBox1.ItemsSource = fileNames;
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            var myButton = sender as Button;
            if (myButton.Tag == null)
            {
                MessageBox.Show("Tag value was null.");
            }
            else
            {
                MessageBox.Show(string.Format("File name is {0}", myButton.Tag));
            }
        }
    }

    public class DownloadModel
    {
        public string FileName { get; set; }
    }

【讨论】:

  • string fileName = myButton.Tag.ToString(); Tagnull。它抛出异常。
  • @Debhere then associated FileName 也是空的,TextBlock 也不应该有任何文件名。在调用ToString() 之前,只需添加一个检查以查看myButton.Tag 是否不为空。
  • 抱歉,我已经解决了,我忘记输入Tag="{Binding Path=FileName}",现在可以使用了。但是如果我在Command Binding中做的话,你能举个例子吗,应该怎么做,考虑到Command Class已经实现了,
  • 关注this example,很简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
相关资源
最近更新 更多