ListView Example

listview.png

The ListView in WPF is typically nearly identical to the ListBox except for its ability to swap in and out custom views. In WPF Composites, you can likely achieve the same effect likely with both the ListBox and the ListView but for simplicity's sake and consistency, I only show the example of customizing a view with a ListView.

The code that does the work resides in the ListViewExt class:
public static void CustomizeView(this ListView chain, Action<Border> transformingAction)
{
    IParent currentComposite = ((ListView)chain).GetSetting<ListBox, IParent>("IParent");
     List<Border> listBoxItems = currentComposite.RetrieveCollection(chain);

     if (listBoxItems != null && listBoxItems.Count > 0)
     {
            ObservableCollection<Border> obslistBoxItems = ((ListBox)chain).GetSetting<ListBox, ObservableCollection<Border>>("ObsCol");

            foreach (Border b in obslistBoxItems)
            {
                  transformingAction(b);
            }
     }
}

Here is an example of this CustomizeView code in action:
//Customize View for ListView - Expand All
myGrid.SubscribeEventOnChild<TextBlock, Grid>(gridguid3, 1, 0, "PreviewMouseDown", new MouseButtonEventHandler((o, ev) => {
                myListView.BorderBrush = Brushes.Purple;
                myListView.CustomizeView(bdr => {
                    customizeListView(bdr, true);
                });
            }));
            
//Customize View for ListView - Collapse All
myGrid.SubscribeEventOnChild<TextBlock, Grid>(gridguid3, 1, 1, "PreviewMouseDown", new MouseButtonEventHandler((o, ev) => {
                myListView.BorderBrush = Brushes.Purple;
                myListView.CustomizeView(bdr =>
                {
                    customizeListView(bdr, false);
                });
            }));

      . . . . . . . .

 private void customizeListView(Border bdr, bool isExpanded)
        {
            string z = bdr.GetKey();
            myListView.GetImage<ListView>(z, 0, 0).Visibility = Visibility.Collapsed;
            myListView.GetTextBlock<ListView>(z, 1, 0).Background = Brushes.LightSalmon;
            bdr.BorderThickness = new Thickness(5);
            bdr.BorderBrush = Brushes.DarkGoldenrod;

            #region Add new group label style

            //step 1
            FrameworkElementFactory factoryLabel = ContemporaryStyle.CreateLabelForGroupItem(34.0D, 334.0D, 17.0D, FontWeights.Bold, new Thickness(5, 0, 0, 0), Brushes.Orange, Brushes.AliceBlue, Brushes.Red, new Thickness(1), HorizontalAlignment.Left);

            //step 2
            FrameworkElementFactory factoryExpander = ContemporaryStyle.CreateExpanderForGroupItem(factoryLabel, isExpanded, Brushes.Green, new Thickness(0, 0, 0, 1), "Orange", "Red", "Blue", "Purple", "Green", "Gold", "Transparent", "M0,0 L0,5 L5,0 z", "M5,5 L0,5 L5,0 z", ExpanderExt.Shape.Rectangle);

            //step 3
            Style myStyle = ContemporaryStyle.CreateStyleForGroupItem(factoryExpander, new Thickness(1));

            //step 4
            GroupStyle groupStyle = new GroupStyle();
            groupStyle.ContainerStyle = myStyle;
            myListView.GroupStyle.Clear();
            myListView.GroupStyle.Add(groupStyle);

            #endregion

            myListView.SetVerticalScrollThumbColors<ListView>("Orange", "Silver", "Gray", "Orange", "0", "0");
        } 

Last edited Jun 13, 2013 at 2:20 AM by stagathome0069, version 9

Comments

No comments yet.