WPF Binding 實驗1

代碼

MainWindow.xaml

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BindingTest" x:Class="BindingTest.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:TextToBooleanConverter x:Key="TextToBooleanConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>

        <Label x:Name="lblValue" Content="0" FontSize="100" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        <CheckBox IsChecked="{Binding Content, Converter={StaticResource TextToBooleanConverter}, ElementName=lblValue, Mode=TwoWay}" Grid.Column="1" x:Name="chkValue" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
            <CheckBox.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="10" ScaleY="10"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </CheckBox.RenderTransform>
        </CheckBox>

        <Button x:Name="btnSet0" Grid.Row="1" Grid.Column="0" Content="0" FontSize="100" Click="btnSet0_Click"/>
        <Button x:Name="btnSet1" Grid.Row="1" Grid.Column="1" Content="1" FontSize="100" Click="btnSet1_Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSet0_Click(object sender, RoutedEventArgs e)
        {
            lblValue.Content = "0";
        }

        private void btnSet1_Click(object sender, RoutedEventArgs e)
        {
            lblValue.Content = "1";
        }
    }
}

TextToBooleanConverter.cs

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

namespace BindingTest
{
    public class TextToBooleanConverter : IValueConverter
    {
        public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            if (value.ToString() == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
        {
            bool b = (bool)value;
            if (b == true)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }
    }
}

運行結果

label爲0時

label爲1時

label的Content屬性(綁定源)與checkbox控件的isChecked屬性(綁定目標)綁定,綁定方向爲雙向,兩者屬性通過TextToBooleanConverter實現數據類型的轉換。

VS 2013(.Net 4.5) 編譯通過。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章