How to Find a Control that is inside DataTemplate & assign Value in WPF?

后端 未结 1 1132
北海茫月
北海茫月 2021-01-29 02:58

I have a DataTemplate which is binded to Grid Group Header Section. There are four TextBlock in DataTemplate from one of TextBlock contains the Grid Header Column Value. Now, I

1条回答
  •  Happy的楠姐
    2021-01-29 03:55

    Since you requested a sample, I am providing a sample using ListBox.
    XAML

    
    
    
    
        
            
                
                    
                        
                            
                            
    
                                
                                    
                                    
                                    
                                
                            
                        
                    
                
            
        
    
    

    CodeBehind

    public partial class DataTemplateWindow : Window {
        public DataTemplateWindow() {
    
            DisplayText = "Some*Text*With*Separators";
            string [] splittedTextArray = DisplayText.Split('*');
            TextBlock0 = splittedTextArray[0];
            TextBlock1 = splittedTextArray[1];
            TextBlock2 = splittedTextArray[2];
    
            ListBoxItems = new List();
            ListBoxItems.Add("Item 1");
    
            InitializeComponent();
            this.DataContext = this;
        }
    
        public string DisplayText { get; set; }
    
        public string TextBlock0 { get; set; }
        public string TextBlock1 { get; set; }
        public string TextBlock2 { get; set; }
    
        public List ListBoxItems { get; set; }
    }
    

    EDIT in response to additional information

    In WPF you can use Element Binding which allows you to access any property of a given element. Since you want to access txtdescription textblock's text property, you will have to use the Element Binding. But you wan't to split that into three TextBlocks. So you will need a converter.

    Use the code below for element binding

    
        
        
        
    
    

    And here is the converter code

    public class SplitterConverter : IValueConverter {
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            string combinedString = value as string;
            if (!string.IsNullOrEmpty(combinedString)) {
                string [] splitArray = combinedString.Split('*');
                int postion = int.Parse(parameter as string);
                switch (postion) {
                    case 0:
                        return splitArray[0];
                    case 1:
                        return splitArray[1];
                    case 2:
                        return splitArray[2];
                    default: 
                        return null;
                }
            }
            return null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new NotImplementedException();
        }
    }
    

    Finally Include Converter Namespace in xaml

    
    
         
    
    ....
    
    

    0 讨论(0)
提交回复
热议问题