1. 程式人生 > >wpf學習-格式化繫結資料

wpf學習-格式化繫結資料

案例目的

     案例是將產品的double型價格轉換為美元型表示

資料模型

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

namespace DataBinding
{
    class Products
    {
        public int ProductID { get; set; }
        public int CategoryID{ get; set; }
        public String ModelNumber{ get; set; }
        public String ModelName{ get; set; }
        public String ProductImageName{ get; set; }
        public double UnitCost { get; set; }
        public String Description { get; set; }

        public Products() { }
        public Products(int _ProductID, int _CategoryID, String _ModelNumber, String _ModelName, String _ProductImageName, double _UnitCost, String _Description)
        {
            ProductID = _ProductID;
            CategoryID = _CategoryID;
            ModelNumber = _ModelNumber;
            ModelName = _ModelName;
            ProductImageName = _ProductImageName;
            UnitCost = _UnitCost;
            Description = _Description;
        }
    }

    class Categories {
        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
        public Categories() { }
        public Categories(int _CategoryID, string _CategoryName) {
            CategoryID = _CategoryID;
            CategoryName = _CategoryName;
        }
    }
}

轉換模型:將double型別轉換為貨幣顯示的格式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;

namespace DataBinding
{
    [ValueConversion(typeof(double),typeof(string))]
    class PriceConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double price = (double)value;
            return price.ToString("C", culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string price = value.ToString().ToString(culture);
            int result;
            if(int.TryParse(price,NumberStyles.Any,culture,out result)){
                return result;
            }
            return value;
        }
    }
}


資料來源模型

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

namespace DataBinding
{
    class ProductsDao
    {
        List<Products> productList = new List<Products>();
        public ProductsDao(){
            InitProducts();
        }
        private void InitProducts() {
            Categories category1 = new Categories(1, "水果");
            Categories category2 = new Categories(2, "乾果");
            Categories category3 = new Categories(3, "小吃");

            Products product1 = new Products(1, 1, "modelNumber1", "modelName1", "./",1.1, "好1");
            Products product2 = new Products(2, 1, "modelNumber2", "modelName2", "./", 1.2, "好2");
            Products product3 = new Products(3, 2, "modelNumber3", "modelName3", "./", 1.3, "好3");
            Products product4 = new Products(4, 2, "modelNumber4", "modelName4", "./", 1.4, "好4");
            Products product5 = new Products(5, 3, "modelNumber5", "modelName5", "./", 1.5, "好5");
            Products product6 = new Products(6, 3, "modelNumber6", "modelName6", "./", 1.6, "好6");
            Products product7 = new Products(7, 3, "modelNumber7", "modelName7", "./", 1.7, "好7");

            productList.Add(product1);
            productList.Add(product2);
            productList.Add(product3);
            productList.Add(product4);
            productList.Add(product5);
            productList.Add(product6);
            productList.Add(product7);
        }
        public Products GetProductByID(int ID) {
            return productList[ID];
        }
        public List<Products> GetAllProducts() {
            return productList;
        }
    }
}

xmal佈局檔案

<Window x:Class="DataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataBinding"
        Title="MainWindow" Height="350" Width="525">
    
    <Grid Background="LightBlue">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"></ColumnDefinition>
            <ColumnDefinition Width="2*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <ListBox x:Name="productList" DisplayMemberPath="ModelName"></ListBox>
        <StackPanel Grid.Column="1" DataContext="{Binding ElementName=productList, Path=SelectedItem}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Model Number :" Margin="5"></TextBlock>
                <TextBox  Margin="5" Width="200" Text="{Binding Path=ModelNumber}"></TextBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Model     Name :" Margin="5"></TextBlock>
                <TextBox  Margin="5" Width="200" Text="{Binding Path=ModelName}"></TextBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Unit           Cost :" Margin="5"></TextBlock>
                <TextBox  Margin="5" Width="200">
                    <TextBox.Text>
                        <Binding Path="UnitCost">
                            <Binding.Converter>
                                <local:PriceConverter></local:PriceConverter>
                            </Binding.Converter>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
            </StackPanel>
            <TextBlock Text="Description :" Margin="5"> </TextBlock>
            <TextBox  Margin="5,5,10,5" MinHeight="140" Text="{Binding Path=Description}"></TextBox>
        </StackPanel>
    </Grid>
</Window>

xaml隱藏程式碼

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;

namespace DataBinding
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            init();
        }
        private void init() { 
            ProductsDao dao = new ProductsDao ();
            productList.ItemsSource = dao.GetAllProducts();
        }
    }
}

結果演示