D22_02_创建自定义视图

<Window x:Class="demo.CustomListViewTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CustomListViewTest" Height="375" Width="525"
        xmlns:local="clr-namespace:demo">
    <Window.Resources>
        <local:ImagePathConverter x:Key="ImagePathConverter"></local:ImagePathConverter>

        <GridView x:Key="GridView">
            <GridView.Columns>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=ModelName}"></GridViewColumn>
                <GridViewColumn Header="Model" DisplayMemberBinding="{Binding Path=ModelNumber}"></GridViewColumn>
                <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Path=UnitCost}"></GridViewColumn>
            </GridView.Columns>
        </GridView>
        <!--自定义视图-->
        <local:TileView x:Key="ImageView">
            <local:TileView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="150" VerticalAlignment="Top">
                        <Image Source="{Binding Path=ProductImagePath,Converter={StaticResource ImagePathConverter}}"></Image>
                        <TextBlock TextWrapping="Wrap" HorizontalAlignment="Center" Text="{Binding Path=ModelName}"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </local:TileView.ItemTemplate>
        </local:TileView>
        <!--自定义视图-->
        <local:TileView x:Key="ImageDetailView" >
            <local:TileView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition Width="Auto" SharedSizeGroup="Col2"></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <Image Margin="5" Width="100" Source="{Binding Path=ProductImagePath,Converter={StaticResource ImagePathConverter}}"></Image>
                        <StackPanel Grid.Column="1" VerticalAlignment="Center">
                            <TextBlock FontWeight="Bold" Text="{Binding Path=ModelName}"></TextBlock>
                            <TextBlock Text="{Binding Path=ModelNumber}"></TextBlock>
                            <TextBlock Text="{Binding Path=UnitCost,StringFormat={}{0:C}}"></TextBlock>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </local:TileView.ItemTemplate>
        </local:TileView>

    </Window.Resources>
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <!--ListView通过View指定视图-->
        <ListView Name="lstProducts" View="{StaticResource GridView}"></ListView>
        <Grid Grid.Row="1" Margin="5">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBlock Margin="5" VerticalAlignment="Center">Choose your view:</TextBlock>
            <!--通过SelectionChanged事件改变视图方式-->
            <ComboBox Grid.Column="1" Margin="5" Width="150" HorizontalAlignment="Left" Name="lstView" SelectionChanged="lstView_SelectionChanged">
                <ComboBoxItem>GridView</ComboBoxItem>
                <ComboBoxItem>ImageView</ComboBoxItem>
                <ComboBoxItem>ImageDetailView</ComboBoxItem>
            </ComboBox>
        </Grid>
    </Grid>
</Window>

CustomListViewTest

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.Shapes;

namespace demo
{
    /// <summary>
    /// CustomListViewTest.xaml 的交互逻辑
    /// </summary>
    public partial class CustomListViewTest : Window
    {
        public CustomListViewTest()
        {
            InitializeComponent();

            lstProducts.ItemsSource = App.StoreDb.GetProducts();
        }

        private void lstView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //把选择项转为ComboBoxItem对象
            ComboBoxItem selectedItem = (ComboBoxItem)lstView.SelectedItem;
            //通过选择项改变listview的视图方式,TryFindResource去xaml中查找资源
            lstProducts.View = (ViewBase)this.TryFindResource(selectedItem.Content);
        }
    }
}

TileView 自定义视图

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace demo
{
    //自定义视图,继承ViewBase,复写DefaultStyleKey,ItemContainerDefaultStyleKey方法
    public class TileView : ViewBase
    {
        private DataTemplate itemTemplate;
        public DataTemplate ItemTemplate
        {
            get { return itemTemplate; }
            set { itemTemplate = value; }
    }

        protected override object DefaultStyleKey
        {
            get
            {
                //TileView资源ID,去themes下generic.xaml文件中查找
                return new ComponentResourceKey(GetType(),"TileView");
            }
        }

        protected override object ItemContainerDefaultStyleKey
        {
            get
            {
                //TileViewItem资源ID,去themes下generic.xaml文件中查找
                return new ComponentResourceKey(GetType(),"TileViewItem");
            }
        }
    }
}

generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:demo">
    <Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:TileView}, ResourceId=TileView}"
         TargetType="{x:Type ListView}" BasedOn="{StaticResource {x:Type ListBox}}">
        <Setter Property="BorderBrush" Value="Black"></Setter>
        <Setter Property="BorderThickness" Value="0.5"></Setter>
        <Setter Property="Grid.IsSharedSizeScope" Value="True"></Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
                     RelativeSource={RelativeSource
                                     AncestorType=ScrollContentPresenter}}"
                   ></WrapPanel>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:TileView},ResourceId=TileViewItem}"
         TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
        <Setter Property="Padding" Value="3"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
        <Setter Property="ContentTemplate" Value="{Binding Path=View.ItemTemplate,
            RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}"></Setter>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border
           Name="Border" BorderThickness="1" CornerRadius="3" >
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Border" Property="BorderBrush"
               Value="{Binding Path=View.SelectedBorderBrush,
            RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}"></Setter>
                            <Setter TargetName="Border" Property="Background"
              Value="{Binding Path=View.SelectedBackground,
            RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}}"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>
</ResourceDictionary>
时间: 2024-07-30 10:11:28

D22_02_创建自定义视图的相关文章

crm2011创建自定义视图

using System; using Microsoft.Xrm.Sdk; /// <summary> /// 创建自定义视图 /// </summary> public class CreateViewHelper { public void Create(IOrganizationService service) { //查询的FetchXml string fetchXml = @"<fetch version='1.0' output-format='xm

UI开发----自定义视图和视图控制器(Controller)

//  Created By 郭仔  2015年04月14日21:34:01 一.自定义视图: 根据需求的不同,?自定义视图继承的类也有所不同.?一般?自定义的 视图会继承于UIView.以下是?自定义视图的要点: 1.创建?一个UIView?子类 2.在类的初始化?方法中添加?子视图 3.类的.h?文件提供?一些接?口(?方法),便于外界操作?子视图 ================== 这里以label-textfield自定义视图为例: 把Label和Textfield封装到LTView中

Android自定义视图与自定义属性

这是Android UI Fundamentals里的内容 创建自定义视图 创建自定义UI组件首先要继承一个视图类. 首先创建一个简单的自定义视图, 展示一条十字线. 需要做的第一件事是创建一个继承自View的CrossView类. public CrossView(Context context, AttributeSet attrs) { super(context, attrs); } 该构造函数的第二个参数是用来传递XML参数的, 等会儿会讲到. 接下来我们要重写两个基础方法: onMe

自定义视图

1.创建一个视图类实现spring的View接口,并且把视图类用@Component注解为sprinmvc组建(重要) package com.hy.springmvc.views; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Compo

iOS7新特性 ViewController转场切换(三) 自定义视图控制器容器的切换---非交互式

@继续前面的内容,这一章,主要介绍自定义ViewController容器上视图VC的切换.先来看看系统给我们提供的容器控制器 UINavigationController和UITabBarController 都有一个NSArray类型的属性viewControllers,很明显,存储的就是需要切换的视图VC.同理,我们定义一个ContainerViewController,是UIViewController的直接子类,用来作为容器依托,额,其他属性定义详见代码吧,这里不多说了.(PS:原先我进

UI基本控件和自定义视图

UILabel 常用属性: UITextField 常用属性: 输入控制属性: 外观控制属性: 输入框让键盘回收的方法: 1.通过协议 (1)让AppDelegate成为输入框的代理对象 ,让TA去执行事件(AppDelegate接受输入框的协议) (2)接受了协议就要执行键盘回收的方法 (3)建立关系: 如:textField.delegate = self;(self指AppDelegate) 2.通过Tag值 (1)创建一个按钮添加触发事件,让Tag值的键盘回收 (2)按钮触发的事件方法如

封装(自定义视图) Encapsulation

封装(自定义视图) //布局注册界面 – 未使用封装版 在AppDelegate.m文件中创建 在下面创建布局界面方法 输入框设置代理之后需要在AppDelegate.m文件的上方延展后加上协议<UITextFieldDelegate> 处理键盘的触发,回收,和点击是否可以编辑 在AppDelegate.m文件中写方法

sharepoint2010 创建自定义列表

sharepoint2010 创建自定义列表 分类: sharepoint20102014-04-04 14:06 106人阅读 评论(0) 收藏 举报 转:http://boke.25k5.com/kan77298.html 如何创建自定义列表 首先了解创建自定义列表中涉及到的几个名词:栏.内容类型. ①栏:栏即列.字段(Field),MSDN中给出的解释为:“字段”一词在 SharePoint Foundation 开发中有两个关系非常密切的含义.有时它指的是列表中的列,但如果提到单个列表项

Android创建自定义dialog方法详解-样式去掉阴影效果

在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说     间接父类是dialog,想了解dialog继承结构可以去百度,或者    从构造器来说ProgressDialog(Context context, int theme)很明显需要个样式主题文件,我们可以在value文件下自定义一个样式文件.   从外观上需要个动态效果控件和文本框两个属性    ProgressBar mProgress;   TextView mMessageView源码中onCreat