开发者

Binding doesn't work but also doesn't give any errors

I am trying to get this code (from a blog) working.

The download file isn't available there so I copied the pieces of the code and it seems to work except that I had to make up a binding but the items do not show up.

Any ideas on how to fix this? Sounds like a trivial thing I am missing.

<Window x:Class="WpfMultiColumnSorting.SortWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SortWindow" Height="300" Width="600">
    <ListView Name="DashListView" ItemsSource="{Binding SortedItems}">

        <ListView.View>
            <GridView>
                <GridViewColumn Width="95" DisplayMemberBinding="{Binding Name}">
                    <GridViewColumnHeader Click="SortClick" Tag="Name" Content="Name" />
                </GridViewColumn>

                <GridViewColumn Width="90">
                    <GridViewColumnHeader Click="SortClick" Tag="IsActive" Content="Activitation" />
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" ToolTip="{Binding IsActive}">
                                <TextBlock Name="ActivityStatusText" Text="{Binding IsActive}"></TextBlock>
                            </StackPanel>
                            <DataTemplate.Triggers>
                                <DataTrigger Binding="{Binding IsActive}" Value="true">
                                    <Setter TargetName="ActivityStatusText" Property="FontWeight" Value="Bold" />
                                </DataTrigger>
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

                <GridViewColumn Width="140" DisplayMemberBinding="{Binding CreationDate}">
                    <GridViewColumnHeader Click="SortClick" Tag="CreationDate" Content="Created On" />
                </GridViewColumn>

                <GridViewColumn Width="85" DisplayMemberBinding="{Binding Value}">
                    <GridViewColumnHeader Click="SortClick" Tag="Value" Content="Value #" />
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Window>





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Globalization;
using System.Collections.ObjectModel;

namespace WpfMultiColumnSorting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class SortWindow : Window
    {
        ObservableCollection<SortedItemViewModel> sortedItems;
        public ObservableCollection<SortedItemViewModel> SortedItems
        {
            get { return this.sortedItems; }
            set
            {
                this.sortedItems = value;
                this.RaisePropertyChanged ( "SortedItems" );
            }
        }

        public SortWindow ( )
        {
            this.SortedItems = new ObservableCollection<SortedItemViewModel> ( );

            this.SortedItems = new ObservableCollection<SortedItemViewModel> ( ) {
                new SortedItemViewModel ( "A", false, DateTime.Now, "1" ),
                new SortedItemViewModel ( "B", false, DateTime.Now, "2" ),
                new SortedItemViewModel ( "C", false, DateTime.Now, "3" ),
                new SortedItemViewModel ( "D", false, DateTime.Now, "4" ),
                new SortedItemViewModel ( "E", false, DateTime.Now, "5" ),
                new SortedItemViewModel ( "F", false, DateTime.Now, "6" ),
                new SortedItemViewModel ( "G", false, DateTime.Now, "7" )
            };
        }

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChanged ( string propertyName )
        {
            var handler = this.PropertyChanged;
            if ( handler != null )
                handler ( this, new PropertyChangedEventArgs ( propertyName ) );
        }

        #endregion

        private List<GridViewColumnHeader> _sortColumnStack = new List<GridViewColumnHeader> ( );

        private IDictionary<GridViewColumnHeader, SortAdorner> _adorners = new Dictionary<GridViewColumnHeader, SortAdorner> ( );


        private void SortClick ( object sender, RoutedEventArgs e )
        {
            GridViewColumnHeader column = sender as GridViewColumnHeader;
            ListSortDirection newDir = ListSortDirection.Ascending;

            // Control means clear out all the prior sort stack
            bool clear = Keyboard.Modifiers == ModifierKeys.Control;

            PerformSort ( column, newDir, clear );
        }


        /// <summary>
        /// Updates the sort stack
        /// </summary>
        /// <param name="column">The columkn on which we are sorting</param>
        /// <param name="newDir">The direction we should place the column sort into</param>
        /// <param name="clear">true to clear out all curent sorting</param>
        private void PerformSort ( GridViewColumnHeader column, ListSortDirection newDir, bool clear )
        {
            IDictionary<GridViewColumnHeader, SortAdorner> oldAdorners = new Dictionary<GridViewColumnHeader, SortAdorner> ( );

            // determine if we are clicking an already sorted column
            if ( _sortColumnStack.Contains ( column ) && _adorners.ContainsKey ( column ) )
            {
                if ( _sortColumnStack [ _sortColumnStack.Count - 1 ] != column )
                {
                    // if it is not the primary use the same direction
                    newDir = _adorners [ column ].Direction;
                }
                else
                {
                    // if it is the primary column then flip it
                    if ( newDir == _adorners [ column ].Direction )
                        newDir = ListSortDirection.Descending;
                }
            }

            // remove all the adorners so we can start a fresh
            foreach ( GridViewColumnHeader col in _adorners.Keys )
            {
                // save into our old adorners list
                oldAdorners [ col ] = _adorners [ col ];
                // then remove it
                AdornerLayer.GetAdornerLayer ( col ).Remove ( _adorners [ col ] );
            }
            _adorners.Clear ( );

            // get rid of all the sorting
            DashListView.Items.SortDescriptions.Clear ( );

            // Control means clear out all the prior sort stack
            if ( clear )
            {
                _sortColumnStack.Clear ( );
            }
            else
            {
                // remove ourselves from the stack if we are in it
                if ( _sortColumnStack.Contains ( column ) )
                {
                    _sortColumnStack.Remove ( column );
                }
            }

            // add our brand new primary one
            _sortColumnStack.Add ( column );

            // now re-create the adorners from our stack
            for ( int i = _sortColumnStack.Count - 1; i >= 0; i-- )
            {
                GridViewColumnHeader col = _sortColumnStack [ i ];
                SortAdorner adorner;

                if ( i == _sortColumnStack.Count - 1 )
                {
                    // create a new one (for the last one)
                    adorner = new SortAdorner ( col, newDir, col.Tag as string, _sortColumnStack.Count - i );
                }
                else if ( oldAdorners.ContainsKey ( col ) )
                {
                    // re-use the old adorner information
                    SortAdorner oldAdorner = oldAdorners [ col 开发者_如何学C];
                    adorner = new SortAdorner ( col, oldAdorner.Direction, col.Tag as string, _sortColumnStack.Count - i );
                }
                else
                {
                    // create a new one (for the last one)
                    adorner = new SortAdorner ( col, newDir, col.Tag as string, _sortColumnStack.Count - i );
                }

                _adorners.Add ( col, adorner );
                AdornerLayer.GetAdornerLayer ( col ).Add ( _adorners [ col ] );
                DashListView.Items.SortDescriptions.Add ( new SortDescription (
                _adorners [ col ].Field, _adorners [ col ].Direction ) );
            }
        }
    }
}





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfMultiColumnSorting
{
    public class SortedItemViewModel : INotifyPropertyChanged
    {
        string name;
        public string Name
        {
            get { return name; }
            set
            {
                this.name = value;
                this.RaisePropertyChanged ( "Name" );
            }
        }

        public bool IsActive { get; set; }
        public DateTime CreationDate { get; set; }
        public string Value { get; set; }

        public SortedItemViewModel ( string name, bool isActive, DateTime creationDate, string value )
        {
            this.Name = name;
            this.IsActive = isActive;
            this.CreationDate = creationDate;
            this.Value = value;
        }

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChanged ( string propertyName )
        {
            var handler = this.PropertyChanged;
            if ( handler != null )
                handler ( this, new PropertyChangedEventArgs ( propertyName ) );
        }

        #endregion
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media;
using System.ComponentModel;
using System.Windows;
using System.Globalization;

namespace WpfMultiColumnSorting
{
    public class SortAdorner : Adorner
    {
        private readonly static Geometry AscGeometry = Geometry.Parse ( "M 0,0 L 8,0 L 4,5 Z" );

        private readonly static Geometry DescGeometry = Geometry.Parse ( "M 0,5 L 8,5 L 4,0 Z" );

        public ListSortDirection Direction { get; private set; }

        public string Field { get; private set; }

        public int SortOrder { get; private set; }

        public SortAdorner ( UIElement element, ListSortDirection dir, string field, int order )
            : base ( element )
        {
            Direction = dir;
            Field = field;
            SortOrder = order;
        }

        protected override void OnRender ( DrawingContext drawingContext )
        {
            base.OnRender ( drawingContext );

            if ( AdornedElement.RenderSize.Width < 20 )
                return;

            drawingContext.PushTransform ( new TranslateTransform (
            AdornedElement.RenderSize.Width - 15, ( AdornedElement.RenderSize.Height - 5 ) / 2 ) );

            drawingContext.DrawGeometry ( SortOrder == 1 ? Brushes.Black : Brushes.DarkGray, null,
            Direction == ListSortDirection.Ascending ? AscGeometry : DescGeometry );

            // annotate each arrow adorner with a number if it is not the first one
            if ( SortOrder != 1 )
            {
                drawingContext.DrawText ( new FormattedText (
                SortOrder.ToString ( ), CultureInfo.GetCultureInfo ( "en-us" ),
                FlowDirection.LeftToRight, new Typeface ( "Verdana" ), 9,
                Brushes.Black ), new Point ( 7, 0 ) );
            }

            drawingContext.Pop ( );
        }
    }
}

No compile errors, or runtime errors and the listview is empty.


It looks like you're not setting the DataContext for the ListView. Create a new class called SortWindowViewModel and move most of your code in SortWindow.xaml.cs to this new class. Add a Loaded event handler in SortWindow.xaml.cs and add DataContext = new SortWindowViewModel(); The bindings look fine, it's just missing where those bindings are pointed to.


I have seen this scenario from time to time in WPF. I have no idea why it happens, but can usually be solved by moving your datatemplates out of the control and into the resources section, and referencing them by name or type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜