繁体   English   中英

如何设置CollectionViewSource的源

[英]How to set the source of a CollectionViewSource

我正在尝试为Microsoft Team Founation Server创建一个小工具(对于这个问题,它并不那么重要)。 但是,我对C#和WPF不太熟悉,即使在通读了一些有关绑定和资源的教程之后,我也不清楚如何使代码正常工作。 使用Visual Studio Designer,我创建了一个小窗体,其中只有一些按钮和一个文本框,应显示授权用户的名称。 不幸的是,登录后会引发ArgumentException 所以问题是:如何将本地TfsTeamProjectCollection绑定到tfsTeamProjectCollectionViewSource 谢谢你的帮助!

<Window
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:local="clr-namespace:TFSBranchingTool"
       xmlns:sys="clr-namespace:System;assembly=mscorlib"
       xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"

       xmlns:Client="clr-namespace:Microsoft.TeamFoundation.Client;assembly=Microsoft.TeamFoundation.Client" x:Class="TFSBranchingTool.MainWindow"

       mc:Ignorable="d"
       Title="TFSBranchingTool" Height="360" Width="560"
       x:Name="wnd" Loaded="wnd_Loaded">
   <Window.Resources>
       <CollectionViewSource x:Key="tfsTeamProjectCollectionViewSource" d:DesignSource="{d:DesignInstance {x:Type Client:TfsTeamProjectCollection}, CreateList=True}" Source="{Binding}"/>
   </Window.Resources>
   <Grid Margin="0,0,0,-1">
       <Menu x:Name="menu" VerticalAlignment="Top" Background="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" d:IsLocked="True">
           <MenuItem Header="File">
               <MenuItem Header="Exit" HorizontalAlignment="Left" Click="exit_application_click"/>
           </MenuItem>
           <MenuItem Header="Team">
               <MenuItem Header="Connect to server" HorizontalAlignment="Left" Margin="0" Width="201" Click="connect_to_server_click"/>
           </MenuItem>
       </Menu>
       <StatusBar x:Name="statusbar" VerticalAlignment="Bottom" Margin="0,0,0,-2" MinHeight="16">
           <StatusBarItem x:Name="connection_status" Content="{Binding TeamProjectCollection.AuthorizedIdentity.DisplayName}" HorizontalAlignment="Left"/>
       </StatusBar>
       <Grid x:Name="grid1" DataContext="{StaticResource tfsTeamProjectCollectionViewSource}" HorizontalAlignment="Left" Margin="138,122,0,0" VerticalAlignment="Top">
           <Grid.ColumnDefinitions>
               <ColumnDefinition Width="Auto"/>
               <ColumnDefinition Width="Auto"/>
           </Grid.ColumnDefinitions>
           <Grid.RowDefinitions>
               <RowDefinition Height="Auto"/>
           </Grid.RowDefinitions>
           <Label Content="Display Name:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
           <TextBox x:Name="displayNameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding AuthorizedIdentity.DisplayName, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
       </Grid>
   </Grid>

这是逻辑:

using Microsoft.TeamFoundation.Client;
using System.Windows;

namespace TFSBranchingTool
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private TfsTeamProjectCollection m_tfs_team_project_collection;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void exit_application_click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void connect_to_server_click(object sender, RoutedEventArgs e)
        {
            TeamProjectPicker team_project_picker = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false);
            if (team_project_picker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                m_tfs_team_project_collection = team_project_picker.SelectedTeamProjectCollection;

                System.Windows.Data.CollectionViewSource tfsTeamProjectCollectionViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tfsTeamProjectCollectionViewSource")));

                //ArgumentException : 
                //Additional Information: "<TFS-URI>" is an invalid value for the property "Source".

                tfsTeamProjectCollectionViewSource.Source = m_tfs_team_project_collection;
            }
        }
    }
}

您必须使用CollectionViewSource类的GetDefaultView静态方法为您的集合提供视图。

这是您要做的。

tfsTeamProjectCollectionViewSource.Source = CollectionViewSource.GetDefaultView(m_tfs_team_project_collection);

另外,您还没有将窗口的数据上下文设置为Window本身。

尝试这样做。

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
}

通过执行上述操作,xaml代码中的任何绑定都将在窗口中查找其源。

我在代码中发现的另一件事是您已将tfsTeamProjectCollectionViewSource定义为局部变量,而不是Window的数据成员。

尝试使其成为像m_tfs_team_project_collection这样的数据成员,然后查看会发生什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM