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

后端 未结 1 1118
北海茫月
北海茫月 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条回答
  • 2021-01-29 03:55

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

    <Window.Resources>
    </Window.Resources>
    
    <Grid>
        <ListBox x:Name="lstBox" ItemsSource="{Binding ListBoxItems}">
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <Border BorderBrush="Black"  BorderThickness="1" Width="1300" DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}">
                        <StackPanel Orientation="Vertical" Margin="2">
                            <TextBlock Name="txtdescription" Text="{Binding DisplayText, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Width="200" HorizontalAlignment="Left" ></TextBlock>
                            <StackPanel Orientation="Horizontal" Margin="2" Height="80">
    
                                <StackPanel Orientation="Vertical" Margin="2">
                                    <TextBlock Name="txtdesc1" Text="{Binding Path=TextBlock0}"/>
                                    <TextBlock Name="txtdesc2" Text="{Binding Path=TextBlock1}"/>
                                    <TextBlock Name="txtdesc3" Text="{Binding Path=TextBlock2}"/>
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
    

    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<string>();
            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<string> 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

    <StackPanel Orientation="Vertical" Margin="2">
        <TextBlock Name="txtdesc1" Text="{Binding ElementName=txtdescription, Path=Text, Converter={StaticResource splitter}, ConverterParameter=0 }"/>
        <TextBlock Name="txtdesc2" Text="{Binding ElementName=txtdescription, Path=Text, Converter={StaticResource splitter}, ConverterParameter=1 }"/>
        <TextBlock Name="txtdesc3" Text="{Binding ElementName=txtdescription, Path=Text, Converter={StaticResource splitter}, ConverterParameter=2 }"/>
    </StackPanel>
    

    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

    <Window x:Class="WpfApplication1.DataTemplateWindow"
        xmlns:cv="clr-yourconverterclassnamespace"
        ...
        >
    <Window.Resources>
         <cv:SplitterConverter x:Key="splitter" />
    </Window.Resources>
    ....
    </Window>
    
    0 讨论(0)
提交回复
热议问题