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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中的控件Binding

發布時間:2023/12/16 C# 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中的控件Binding 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C#中的控件Binding

  • 1、 對于普通string類型(例如一個TextBox值隨一個值得變化而變化)

  • 1.1窗口繼承INotifyPropertyChanged接口

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
    }

1.2界面中進行綁定

<TextBlock Text="{ Binding UserName}"/>

1.3定義綁定值的get set方法(名稱要一樣)

private string userName; //放在類外public string UserName{get{return userName;}set{userName = value;OnPropertyChanged();}}

1.4定義事件發生函數

public event PropertyChangedEventHandler PropertyChanged;private void OnPropertyChanged([CallerMemberName]string prop = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));}

//UserName值發生變化觸發OnPropertyChanged()函數,此函數中觸發PropertyChanged事件
//該事件已在內部訂閱,即為UI綁定改變事件
備注:([CallerMemberName]string prop = “”)采用此方法,則某一值發生改變時不用手動傳遞該值名稱,會自動獲取。避免傳遞名稱填錯導致綁定聯系不上

1.5定義上下文

DataContext = this;

//this表示PropertyChanged事件觸發的綁定事件在此.cs文件內

  • 2、 對于List類型
  • 2.1定義ObservableCollection列表,此類列表中已經繼承了INotifyPropertyChanged接口

public ObservableCollection<C_UserInfo > UserInfos { get; set; }

2.2表明上下文

DataContext = this;

2.3為列表賦值

UserMgr.Default.UserList()?.ForEach(o =>{UserInfos.Add(o);});

2.4UI中使用listView控件并進行綁定

<ListView Grid.Row="2" Name="UserList" ItemsSource="{Binding UserInfos}" MouseDoubleClick="UserList_MouseDoubleClick"><ListView.Template><ControlTemplate><ScrollViewer Background="AliceBlue" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Hidden"><ItemsPresenter/></ScrollViewer></ControlTemplate></ListView.Template><ListView.ItemTemplate><DataTemplate><Grid><Grid.ColumnDefinitions><ColumnDefinition Width=".3*"/><ColumnDefinition Width=".7*"/></Grid.ColumnDefinitions><Image Name="UserUnifyIcon"/><StackPanel Orientation="Vertical" Grid.Column="1"><StackPanel Orientation="Horizontal" VerticalAlignment="Center"><TextBlock Text="{Binding UserName}"/><TextBlock Text="{Binding Status, Converter={StaticResource Bool2StatusCvt}}"/></StackPanel><StackPanel Orientation="Horizontal" VerticalAlignment="Center"><TextBlock Text="{Binding Account}"/><TextBlock Text="{Binding Sex,Converter={StaticResource SexType2StringCvt}}"/> </StackPanel></StackPanel></Grid></DataTemplate></ListView.ItemTemplate></ListView>

//listview綁定ObservableCollection列表,其內控件的值可綁定自定義列表中屬性名稱(名稱要一致)

備注1:List只對列表增刪有binding效果,綁定類屬性定義也要寫get set方法
備注2:若要對列表里項元素更新做監視,則ObservableCollection列表中的元素類型要做監視屬性,即定義C_UserInfo時有要求。所以一般會做類型轉換,將普通類型結構體與專門做界面綁定的結構體做類型轉化

public class C_UserInfo : INotifyPropertyChanged{private bool status; //狀態private string account; //賬號private string userName; //姓名private string password; //密碼public bool Status //定義屬性中get set方法{get => status;set{status = value;OnPropertyChanged();}}public string Account{get => account;set{account = value;OnPropertyChanged();}}public string UserName{get => userName;set{userName = value;OnPropertyChanged();}}public event PropertyChangedEventHandler PropertyChanged;private void OnPropertyChanged([CallerMemberName]string prop = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));} }

備注3:綁定的時候出現類型轉換,例如結構體里面屬性是布爾值,但需要顯示(在線/離線)
1、 定義一個文件夾,文件夾下是各種轉換類
2、類繼承 IValueConverter

class Bool2StatusCvt : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){var str = String.Empty;if(value is bool){bool b = (bool)value;str = b ? "在線" : "離線";return str;}return str;}

3、創建一個資源字典,添加轉換類

xmlns:cvt="clr-namespace:GuGuBirdClient.Cvt"> //把轉換類命名空間加進去<cvt:Bool2StatusCvt x:Key="Bool2StatusCvt"/> //cvt就是該類命名空間 x:是主空間,并創造一個唯一的key值

4、在app.xml中添加資源字典
<

ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="StyleDictionary.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary>

5、在要用綁定的XAML中,引用key值

<TextBlock Text="{Binding UserStatus,Converter={StaticResource Bool2StatusCvt}}" Margin="5,10,0,0"/>

//引用的是key值,binding還是照之前的寫

總結

以上是生活随笔為你收集整理的C#中的控件Binding的全部內容,希望文章能夠幫你解決所遇到的問題。

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