WPF - ItemsControl

时间:2017-01-25 15:09:41

标签: c# wpf data-binding

我做了一些非常糟糕的事情,因为我认为我不应该让它一直工作。我想我应该这样做。

我正在编写一个小测试工具,用于帮助我的测试团队测试我编写的其他服务的使用,但为了测试服务,我需要一个客户端。

基本上我有一个我正在向用户显示的推文列表,我希望他们能够点击推文找出通过推特发送的原始原始有效负载。

我的XAML如下:

                        <ItemsControl x:Name="ActivitiesList" >
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Border BorderThickness="1" BorderBrush="Black" Margin="2">
                                        <Grid Height="100">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="20"/>
                                                <RowDefinition Height="50"/>
                                                <RowDefinition Height="20"/>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="70"/>
                                                <ColumnDefinition Width="40"/>
                                                <ColumnDefinition Width="1*"/>
                                                <ColumnDefinition Width="300"/>
                                            </Grid.ColumnDefinitions>
                                            <Image  Width="Auto" Height="Auto" Source="{Binding Avatar}" Grid.ColumnSpan="1" Grid.RowSpan="2" Grid.Column="0" Grid.Row="0" Margin="2,2,2,2" />
                                            <TextBlock Text="{Binding Author}" Grid.ColumnSpan="3" Grid.RowSpan="1" Grid.Column="1" Grid.Row="0" />
                                            <TextBlock Text="{Binding Id}" Grid.ColumnSpan="1" Grid.RowSpan="1" Grid.Column="3" Grid.Row="0" Margin="0,0,0,0" />
                                            <Border BorderThickness="0" x:Name="border" Grid.ColumnSpan="3" Grid.RowSpan="1" Grid.Column="1" Grid.Row="1"  Margin="0.5" />
                                            <TextBlock Text="{Binding Body}" TextAlignment="Left" TextWrapping="Wrap" Grid.ColumnSpan="3" Grid.RowSpan="1" Grid.Column="1" Grid.Row="1"  Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}"  />
                                            <TextBlock Text="Tags:" Grid.ColumnSpan="1" Grid.RowSpan="1" Grid.Column="0" Grid.Row="2" />
                                            <ItemsControl ItemsSource="{Binding Tags}" Grid.ColumnSpan="2" Grid.Row="2" Grid.Column="1" Margin="0,0,0,0">
                                                <ItemsControl.ItemsPanel>
                                                    <ItemsPanelTemplate>
                                                        <StackPanel Orientation="Horizontal"/>
                                                    </ItemsPanelTemplate>
                                                </ItemsControl.ItemsPanel>
                                                <ItemsControl.ItemTemplate>
                                                    <DataTemplate>
                                                        <Border BorderThickness="1,1,1,1" BorderBrush="Black" CornerRadius="5" Margin="1,1,1,1" >
                                                            <Border.Background>
                                                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                                                    <GradientStop Color="#FFFFEEEE" Offset="0"/>
                                                                    <GradientStop Color="#FFFFEBEB" Offset="1"/>
                                                                </LinearGradientBrush>
                                                            </Border.Background>
                                                            <TextBlock Text="{Binding}" Margin="3,0,3,0"  />
                                                        </Border>
                                                    </DataTemplate>
                                                </ItemsControl.ItemTemplate>
                                            </ItemsControl>
                                            <Button Content="View Raw Data" Grid.Column="3" Grid.Row="2" HorizontalAlignment="Right" Width="105" Tag="{Binding}" Click="ButtonBase_OnClick" />
                                        </Grid>
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <!--<local1:Tweet Id="tag:search.twitter.com,2005:823502379551248384 ( 1000 )" Author="jim parslow" Body="This is the content and is going to be very long so that it takes over the page more and more and more" Avatar="https://pbs.twimg.com/profile_images/539113272553648128/sWcgnCan_normal.jpeg" >
                                <local1:Tweet.Tags>
                                    <System:String>Tag 1</System:String>
                                    <System:String>Tag 2</System:String>
                                    <System:String>Tag 3</System:String>
                                </local1:Tweet.Tags>
                            </local1:Tweet>
                            <local1:Tweet Id="tag:search.twitter.com,2005:823502379551248384 ( 1001 )" Author="jim parslow" Body="This is the content balh" Avatar="https://pbs.twimg.com/profile_images/539113272553648128/sWcgnCan_normal.jpeg" />
                            <local1:Tweet Id="tag:search.twitter.com,2005:823502379551248384 ( 1002 )" Author="jim parslow" Body="This is the content blah 2" Avatar="https://pbs.twimg.com/profile_images/539113272553648128/sWcgnCan_normal.jpeg" />-->
                        </ItemsControl>

我添加了一个按钮,并将整个datacontext项附加到按钮的tag属性中。

<Button Content="View Raw Data" Grid.Column="3" Grid.Row="2" HorizontalAlignment="Right" Width="105" Tag="{Binding}" Click="ButtonBase_OnClick" />

然后在代码中我点击按钮点击:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    var buttonClicked = (Button) sender;
    var tag = buttonClicked.Tag;

    Console.WriteLine("");
}

我尝试过使用带命令的按钮,但无法开始工作。

我尝试使用其他控件中的事件并在网格上添加鼠标点击,但我再次无法使其工作。

目前所有内容都在一个xaml文件和代码文件中(我知道!!)

推文类如下:

public class Tweet : INotifyPropertyChanged
{
    public string Id { get; set; }
    public string Author { get; set; }
    public string Body { get; set; }
    public string Avatar { get; set; }
    public List<string> Tags { get; set; }
    public Activity OriginalActivity { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged;
}

每当我收到一条新推文时,我都会将其添加到列表中并设置列表:

ActivitiesList.ItemsSource = Tweets;

推文是一个可观察的集合

private ObservableCollection<Tweet> _tweets = new ObservableCollection<Tweet>(); 
public ObservableCollection<Tweet> Tweets 
{
    get { return _tweets; }
}

如果有人能告诉我正确的方法,我会永远感激。

编辑*正如要求一个如何做到这一点的例子想知道我尝试了什么:

<Border.InputBindings>
  <MouseBinding MouseAction="LeftClick" Command="{Binding DataContext.ShowData, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/>
</Border.InputBindings>

我也试过这个:

<Button Command="{Binding 
    RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, 
    Path=DataContext.ShowData}" CommandParamater="{Binding}" />

但永远无法在推文上调用该属性

public ICommand ShowData
  {
    get;
    private set;
  }

**

另一个编辑:为什么这不成功?

**

我在ItemsControl中有这个按钮

<Button Content="View Raw Data" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.ShowDataCommand}" CommandParameter="{Binding}" />

在代码中我有以下内容:

public class Tweet : INotifyPropertyChanged
{
    public string Id { get; set; }
    public string Author { get; set; }
    public string Body { get; set; }
    public string Avatar { get; set; }
    public List<string> Tags { get; set; }
    public Activity OriginalActivity { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged;

    RelayCommand _showData;
    public ICommand ShowDataCommand
    {
        get
        {
            if (_showData == null)
            {
                _showData = new RelayCommand(param => this.ShowData((Tweet)param));
            }
            return _showData;
        }
    }

    public void ShowData(Tweet tweet)
    {
        Debug.WriteLine("I AM HERE");
    }
}

public class RelayCommand : ICommand
{
    #region Fields
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;
    #endregion // Fields
    #region Constructors
    public RelayCommand(Action<object> execute) : this(execute, null) { }
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute; _canExecute = canExecute;
    }
    #endregion // Constructors
    #region ICommand Members
    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    public void Execute(object parameter) { _execute(parameter); }
    #endregion // ICommand Members
}

为什么这不起作用?

我没有收到任何错误或消息,说我有任何错误。

有人可以帮忙吗?

0 个答案:

没有答案