码迷,mamicode.com
首页 > 其他好文 > 详细

Bind、Binding與Converter的使用

时间:2015-09-25 18:12:02      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

Demo源碼下載:http://yunpan.cn/cHuCmI4NK4xwr  访问密码 8201

1.Bind

Bind的使用方式是:

<Button Content="{x:Bind Path=xxxxx}" />

其中xxxx是Page所在的class的成員。如:

class YourPage : Page {
public string xxxx {get;set;}
}

Path可以省略不寫。即:

<Button Content={x:Bind xxxx} />

如果x:Bind使用在 ItemTemplate 中,則在 DateTemplate 屬性中必須指定 x:DateType。如:

<Page.Resource>
    <DateTemplate 
        x:Name="template1" 
        x:DateType="local:ItemType">
        <Grid>
            <TextBlock Text="{x:Bind Title}" />
        </Grid>
    </DateTemplate>
</Page.Resource>

其中local為xaml中所使用的名字空間替代者。而我們要聲明一個類:

public class ItemType {
public string Title {get;set;}
}

如此,就可以將ItemTemplate與一個具體的類型綁定了。

2.Binding

Binding的使用方式最靈活。因為它可以指定使用某些控件變量。比如,我們有一個Slider和一個TextBox,Slider的值會隨著TextBox的值的改變而改變,或是TextBox的值隨著Slider的滑動而改變,或者誰的值改變,都會改變對方的值,這個時候,我們就必須要使用Binding了。如:

<Grid>
    <TextBox
        x:Name="txtBox" 
        Text="{Binding ElementName=slider, Path=Value, Mode=TwoWay}" />
    <Slider x:Name="slider" />
</Grid>

ElemenetName指定了使用哪個控件,Path指定使用該控件的哪個屬性,Mode指定了綁定了方式。上面的這段代碼的意思主是:

txtBox.Text = slider.Value.ToString();

slider.Value = int.Parse(txtBox.Text);

因為Mode=TwoWay,表示是雙向綁定,即我的就是你的,你的就是我的。

Mode總共有三個屬性:

1.OneTime只綁定一次

2.OneWay單向綁定,即被綁定的對象值改變時,我就改變,而我改變時不需要去改變它。

3.TwoWay雙向綁定,我改變你也改變,你改變我也改變。

 

如果在ItemTemplate中使用Binding,直接使用類的成員變量即可。如:

<Page.Resource>
    <DateTemplate 
        x:Name="template1" 
        >
        <Grid>
            <TextBlock Text="{Binding Title}" />
        </Grid>
    </DateTemplate>
</Page.Resource>

注意與x:Bind的比較,DateTemplate 是不需要 x:DataType 屬性的。

3.Converter

顧名思義,Converter就是轉換器。它的作用是將控件的某的屬性的值,根據用戶的某些需求而轉換成用戶需要的值。

打個比方說,大陸的一款軟件,上面全是簡化字,當台灣、香港、澳門或是海外華人使用時,會很彆扭,或是認不出來是什麼字,這個時候我們需要一個轉換器,將簡化字轉換成繁體字,還可以根據地區轉換不同的繁體漢字。

又比方說,一個任務軟件,它有任務程度:允許暫緩、標準、緊急。我們的Grid的背景顏色,可以根據這些不同的程度,進行加亮、置暗或使用高亮顏色進行提醒用戶。

這些都可以使用轉換器進行解決。

我寫的一個例子如下:

XAML:

<Page>
    <Page.Resources>
        <local:X2YConverter x:Name="x2yConverter" />
    </Page.Resources>

    <Grid>
        <TextBox Text="{Binding Converter={StaticResource x2yConverter}" />
    </Grid>
</Page>

要在Resources中聲明一個X2YConverter對象。

聲明類X2YConverter:

    class X2YConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string trueValue = value as string;

            string newValue = trueValue.ToLower();

            newValue = newValue.Replace("1", "");
            newValue = newValue.Replace("2", "");
            newValue = newValue.Replace("3", "");
            newValue = newValue.Replace("4", "");
            newValue = newValue.Replace("5", "");
            newValue = newValue.Replace("6", "");
            newValue = newValue.Replace("7", "");
            newValue = newValue.Replace("8", "");
            newValue = newValue.Replace("9", "");
            newValue = newValue.Replace("0", "");

            return newValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

上面這個類簡單地實現了把阿拉伯數字轉換成漢字零到九。

仔細觀看Convert函數,發現它還有 parameter 和 language 函數,這兩個函數均在Binding中指定。如:

<TextBlock Text="{Binding Converter={StaticResource x2yConverter}, ConverterParameter=hehe, ConverterLanguage=chinese" />

最後,還看到一個沒有實現的 ConvertBack 。這個函數只有當綁定模式為 TwoWay 時才有用。因為當它綁定到另一個控件時,這兩個控件的值是你轉換成我,我轉換成你,一個用於你轉換成我,另一個用於我轉換成你。這種情況最適合用在值不同類型的綁定上。如:將一個Color為0xffff0000轉換成字符串“紅色”時,返回類型一個是 Color,另一個是 string。這個是需要注意的。具體的實例可以參見我上一片博客中,關於播放電影中Position與Slider的值綁定。

4.附加.ItemTemplateSelector

這個用於根據某些屬性來改變Item的DataTemplate。比如換膚就可以用這個玩意。

XAML:

<Page>
    <Page.Resource>
         <local:ListItemTemplateSelector x:Name="listItemSelector" 
                                        T1="{StaticResource listtemplateOption1}"
                                        T2="{StaticResource listtemplateOption2}"/>
        <DataTemplate x:Name="listtemplateOption1" x:DataType="local:MyListItem">
            <Grid Width="300">
                <TextBlock Text="{x:Bind Title}" />
                <Button Content="selector1" HorizontalAlignment="Right" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Name="listtemplateOption2">
            <Grid Width="300">
                <TextBlock Text="{Binding}" Foreground="Red"/>
                <Button Content="selector2" HorizontalAlignment="Right" />
            </Grid>
        </DataTemplate>
    </Page.Resources>
    </Page.Resource>


     <Grid>
              <ListView
            ItemsSource="{x:Bind MultiItems}"
            ItemTemplateSelector="{StaticResource listItemSelector}"
                 />
  
     </Grid>
</Page>

定義MyListItem和DataTemplateSelector:

public class MyListItem
    {
        public string Title { get; set; }
    }

public class ListItemTemplateSelector : DataTemplateSelector {
        public DataTemplate T1 { get; set; }
        public DataTemplate T2 { get; set; }

        protected override DataTemplate SelectTemplateCore(System.Object item)
        {
            if (item.GetType() == typeof(MyListItem))
            {
                return T1;
            }

            return T2;
        }
    }

然後弄個List,裡面包括string 和 MyListItem 兩種類型,可以根據不同的類型使用不同的模板進行顯示。截圖如下:

技术分享

 

Bind、Binding與Converter的使用

标签:

原文地址:http://www.cnblogs.com/lin277541/p/4838793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!