日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF中的数据绑定Data Binding使用小结

發布時間:2023/12/10 asp.net 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF中的数据绑定Data Binding使用小结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

完整的數據綁定的語法說明可以在這里查看:

http://www.nbdtech.com/Free/WpfBinding.pdf

MSDN資料:

Data Binding: Part 1?http://msdn.microsoft.com/en-us/library/aa480224.aspx

Data Binding: Part 2?http://msdn.microsoft.com/en-us/library/aa480226.aspx

Data Binding Overview?http://msdn.microsoft.com/en-us/library/ms752347.aspx

?

??????INotifyPropertyChanged接口???綁定的數據源對象一般都要實現INotifyPropertyChanged接口。

?????{Binding}? 說明了被綁定控件的屬性的內容與該控件的DataContext屬性關聯,綁定的是其DataContext代表的整個控件的內容。如下:
<ContentControl Name="LongPreview" Grid.Row="2" Content="{Binding}" HorizontalAlignment="Left"/>
ContentControl 只是一個純粹容納所顯示內容的一個空控件,不負責如何具體顯示各個內容,借助于DataTemplate可以設置內容的顯示細節。

???? 使用參數Path

(使用父元素的DataContext)使用參數綁定,且在數值變化時更新數據源。(兩種寫法)

<TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox.Text>
<Binding Path="StartPrice" UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>

???? 相對資源RelativeSource

RelativeSource={RelativeSource Self}是一個特殊的綁定源,表示指向當前元素自己。自己綁定自己,將ToolTip屬性綁定到Validation.Errors中第一個錯誤項的錯誤信息(Validation.Errors)[0].ErrorContent。

<Style x:Key="textStyleTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="#333333" />
<Setter Property="MaxLength" Value="40" />
<Setter Property="Width" Value="392" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>

?????使用轉換器和驗證規則

<TextBox Name="StartDateEntryForm" Grid.Row="3" Grid.Column="1" Style="{StaticResource textStyleTextBox}" Margin="8,5,0,5" Validation.ErrorTemplate="{StaticResource validationTemplate}">
<TextBox.Text>
<Binding Path="StartDate" UpdateSourceTrigger="PropertyChanged" Converter="{StaticResource dateConverter}">
<Binding.ValidationRules>
<src:FutureDateRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>

//自定義的驗證規則須從ValidationRule繼承。
class FutureDateRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
DateTime date;
try
{
date = DateTime.Parse(value.ToString());
}
catch (FormatException)
{
return new ValidationResult(false, "Value is not a valid date.");
}
if (DateTime.Now.Date > date)
{
return new ValidationResult(false, "Please enter a date in the future.");
}
else
{
return new ValidationResult(true, null);
}
}
}

?????使用數據觸發器

SpecialFeatures是一個枚舉數據類型。

<DataTrigger Binding="{Binding Path=SpecialFeatures}">
<DataTrigger.Value>
<src:SpecialFeatures>Color</src:SpecialFeatures>
</DataTrigger.Value>
<Setter Property="BorderBrush" Value="DodgerBlue" TargetName="border" />
<Setter Property="Foreground" Value="Navy" TargetName="descriptionTitle" />
<Setter Property="Foreground" Value="Navy" TargetName="currentPriceTitle" />
<Setter Property="BorderThickness" Value="3" TargetName="border" />
<Setter Property="Padding" Value="5" TargetName="border" />
</DataTrigger>

??? 多重綁定

??? 綁定源是多個源,綁定目標與綁定源是一對多的關系。

<ComboBox.IsEnabled>
<MultiBinding Converter="{StaticResource specialFeaturesConverter}">
<Binding Path="CurrentUser.Rating" Source="{x:Static Application.Current}"/>
<Binding Path="CurrentUser.MemberSince" Source="{x:Static Application.Current}"/>
</MultiBinding>
</ComboBox.IsEnabled>
//自定義的轉換器須實現IMultiValueConverter接口。
class SpecialFeaturesConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//數組中的對象數值的索引順序與XAML文件的多重綁定定義有關。
int rating = (int)values[0];
DateTime date = (DateTime)values[1];

return ((rating >= 10) && (date.Date < (DateTime.Now.Date - new TimeSpan(365, 0, 0, 0))));
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return new object[2] { Binding.DoNothing, Binding.DoNothing };
}
}

??? Master-Detail:主-從應用(使用CollectionViewSource)

主從應用

說明:
AuctionItems的定義為:public ObservableCollection<AuctionItem> AuctionItems? 。
在 ListBox 中直接使用 CollectionViewSource 來表示主數據(AuctionItem集合),在 ContentControl 中則同時使用設定 Content 和 ContentControl 兩個屬性, Content 直接指向CollectionViewSource, ContentControl 則使用先前已經定義的數據模板綁定(數據模板中的數據項則是綁定到AuctionItem類的各個屬性)。

???? 數據分組(使用CollectionViewSource)
分組表頭項的數據模板:
<DataTemplate x:Key="groupingHeaderTemplate">
??? <TextBlock Text="{Binding Path=Name}" Foreground="Navy" FontWeight="Bold" FontSize="12" />
</DataTemplate>
在ListBox中使用分組:
<ListBox Name="Master" Grid.Row="2" Grid.ColumnSpan="3" Margin="8" ItemsSource="{Binding Source={StaticResource listingDataView}}">
??? <ListBox.GroupStyle>
??????? <GroupStyle HeaderTemplate="{StaticResource groupingHeaderTemplate}"/>
??? </ListBox.GroupStyle>
</ListBox>
分組開關:
<CheckBox Name="Grouping" Grid.Row="1" Grid.Column="0" Margin="8" Style="{StaticResource checkBoxStyle}" Checked="AddGrouping" Unchecked="RemoveGrouping">Group by category</CheckBox>
CheckBox的事件處理:
private void AddGrouping(object sender, RoutedEventArgs e)
{
??? PropertyGroupDescription pgd = new PropertyGroupDescription();
??? pgd.PropertyName = "Category"; //使用屬性Category的數值來分組
??? listingDataView.GroupDescriptions.Add(pgd);
}
private void RemoveGrouping(object sender, RoutedEventArgs e)
{
??? listingDataView.GroupDescriptions.Clear();
}

???? 排序數據(使用CollectionViewSource) 比分組簡單
排序開關:
<CheckBox Name="Sorting" Grid.Row="1" Grid.Column="3" Margin="8" Style="{StaticResource checkBoxStyle}" Checked="AddSorting" Unchecked="RemoveSorting">Sort by category and date</CheckBox>
CheckBox的事件處理:
private void AddSorting(object sender, RoutedEventArgs e)
{
??? listingDataView.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending));
??? listingDataView.SortDescriptions.Add(new SortDescription("StartDate", ListSortDirection.Descending));
}
private void RemoveSorting(object sender, RoutedEventArgs e)
{
??? listingDataView.SortDescriptions.Clear();
}

???? 過濾數據(使用CollectionViewSource) 跟排序類似
過濾開關:
<CheckBox Name="Filtering" Grid.Row="1" Grid.Column="1" Margin="8" Style="{StaticResource checkBoxStyle}" Checked="AddFiltering" Unchecked="RemoveFiltering">Show only bargains</CheckBox>
CheckBox的事件處理:
private void AddFiltering(object sender, RoutedEventArgs e)
{
??? listingDataView.Filter += new FilterEventHandler(ShowOnlyBargainsFilter);
}
private void RemoveFiltering(object sender, RoutedEventArgs e)
{
??? listingDataView.Filter -= new FilterEventHandler(ShowOnlyBargainsFilter);
}
private void ShowOnlyBargainsFilter(object sender, FilterEventArgs e)
{
??? AuctionItem product = e.Item as AuctionItem;
??? if (product != null)
??? {
??????? //設置e.Accepted的值即可
??????? e.Accepted = product.CurrentPrice < 25;
??? }
}

轉載于:https://www.cnblogs.com/sjqq/p/7806691.html

總結

以上是生活随笔為你收集整理的WPF中的数据绑定Data Binding使用小结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。