av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

如何使用C#代碼實(shí)現(xiàn)DataTemplate

不得不說DataTemplate是WPF中的一項(xiàng)重要技術(shù)組成,可以方便讓我們實(shí)現(xiàn)各種各樣的UI樣式,大大豐富了應(yīng)用程序的界面設(shè)計(jì)。通常我們都會在XAML文件中定義出具體的DataTemplate, 我舉一個ListView的例子:

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為倉山等服務(wù)建站,倉山等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為倉山企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

Foreground="White" ItemsSource="{Binding}" Style="{DynamicResource

SuperListViewStyle}" ItemContainerStyleSelector="{StaticResource

listViewItemStyleSelector}" x:Name="lvExecutionTable" >
                       
                           
                               
                                   
                                       
                                           

IsChecked="{Binding Path=IsSelected}"/>
                                       
                                   
                                   

Tag="IsSelected"/>
                               
                               
                                   
                                       
                                           

Foreground="{Binding Path=IsComfirmed, Converter={StaticResource

IsComfirmedToColorConverter}}"/>
                                       
                                   
                                   

Tag="IsComfirmed"/>
                               
                               
                                   

Tag="MarketIndicator"/>
                               
                               
                                   
                                       
                                           
                                       

                                   

                                   

Tag="TradeType"/>
                               
                           
                       
                   

例子中對每個Column定義了簡單的DataTemplate。我遇到的問題是如果這個List View需要動態(tài)編輯列,或者新加入一些列,就是如何用代碼來實(shí)現(xiàn)對控件的DataTemplate的操作。經(jīng)過查閱MSDN的相關(guān)文檔,最好找到了解決的辦法,如下:

GridViewColumn column = new GridViewColumn();
            GridViewColumnHeader h = new GridViewColumnHeader();
            h.Content = "??????";
            h.Tag = "*******";
            column.Header = h;
            DataTemplate dt = new DataTemplate();
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof

(CheckBox));
            Binding binding = new Binding();
            binding.Path = new PropertyPath("MarketIndicator");
            fef.SetBinding(CheckBox.ContentProperty, binding);
            fef.SetValue(CheckBox.ForegroundProperty, Brushes.White);
            dt.VisualTree = fef;
            column.CellTemplate = dt;

            (this.lvExecutionTable.View as GridView).Columns.Add(column);

DataTemplate對象的VisualTree屬性的類型是FrameworkElementFactory,用來存放具體的Control。FrameworkElementFactory fef = new FrameworkElementFactory(typeof(CheckBox))語句定義了DataTemplate的頂層元素是一個CheckBox控件,使用fef.SetValue方法可以設(shè)置頂層元素的屬性,如綁定等等。如果你需要定義的DataTemplate不止一個層次,那么你就要花點(diǎn)時間去設(shè)置頂層元素的Content屬性。

【編輯推薦】

  1. 淺析C#3.0編碼習(xí)慣與命名規(guī)則
  2. 詳解C# 4.0中必選參數(shù)與可選參數(shù)混合的問題
  3. 深入解析C#編程中的方法重載

網(wǎng)站標(biāo)題:如何使用C#代碼實(shí)現(xiàn)DataTemplate
網(wǎng)站鏈接:http://uogjgqi.cn/article/djcighh.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流