如何找到一个控件,里面的DataTemplate&安培;在WPF赋值? [英] How to Find a Control that is inside DataTemplate & assign Value in WPF?

查看:282
本文介绍了如何找到一个控件,里面的DataTemplate&安培;在WPF赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被绑定到网格组页眉节一个DataTemplate。有四个TextBlock的在DataTemplate中从TextBlock中的一个包含网格标题列的值。现在,我想拆分此TextBlock的价值分为三,并从代码隐藏分配该值三个其他的TextBlock。 ?是否有可能

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 want to Split this TextBlock Value into three and assign this value to other three TextBlock from Code Behind. Is it Possible?

<DataTemplate x:Key="descriptionHeader">
            <!--<dxg:GroupGridRowContent>
                <TextBlock Background="Yellow" Text="{Binding DisplayText}" ></TextBlock>
            </dxg:GroupGridRowContent>-->

            <Border BorderBrush="Black"  BorderThickness="1" Width="1300">
                <StackPanel Orientation="Vertical" Margin="2">
                    <TextBlock Name="txtdescription" Text="{Binding DisplayText}" Width="200" HorizontalAlignment="Left" ></TextBlock>
                    <StackPanel Orientation="Horizontal" Margin="2" Height="80">

                        <Image Source=".\Images\description_img.png"  Stretch="None" FlowDirection="LeftToRight" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="1"/>
                        <StackPanel Orientation="Vertical" Margin="2">
                            <TextBlock Name="txtdesc1" Margin="2" FlowDirection="LeftToRight" Text="{Binding Path=GlassType,RelativeSource={RelativeSource Self}}"  TextWrapping="Wrap"  />
                            <TextBlock Name="txtdesc2" Margin="2" FlowDirection="LeftToRight" Text="{Binding Path=(dxg:RowData.RowData).GroupSummaryData[3].Text, RelativeSource={RelativeSource Self}}"  TextWrapping="Wrap"  />
                            <TextBlock Name="txtdesc3" Margin="2" FlowDirection="LeftToRight" Text="{Binding Path=(dxg:RowData.RowData).GroupSummaryData[4].Text, RelativeSource={RelativeSource Self}}"   TextWrapping="Wrap"  />
                        </StackPanel>
                    </StackPanel>
            </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>

  <dxg:GridControl Name="grdInfill"  Height="700" VerticalAlignment="Top" >
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="GlassType" AllowEditing="False"   />
            <dxg:GridColumn FieldName="GlassDescription" GroupValueTemplate="{StaticResource descriptionHeader}">
                <!--GroupValueTemplate="{StaticResource descriptionHeader}"-->
                <!--Header="GlassDescription" DisplayMemberBinding="{Binding Path=RowData.Row.GlassDescription, Mode=TwoWay}"-->
            </dxg:GridColumn>
            <dxg:GridColumn FieldName="GlassType" AllowEditing="False" />
            <dxg:GridColumn Name="qty" Header="Quantity" AllowEditing="False" DisplayMemberBinding="{Binding Path=RowData.Row.Quantity, Mode=TwoWay}" /> <!--FieldName="Quantity"-->
            <dxg:GridColumn FieldName="Width" AllowEditing="False" Header="Length"/>
            <dxg:GridColumn FieldName="Height" AllowEditing="False"/>
            <dxg:GridColumn FieldName="Elevation" AllowEditing="False"/>
            <dxg:GridColumn FieldName="Mark" AllowEditing="False"/>
            <dxg:GridColumn FieldName="GlassTag" AllowEditing="False"/>
            <dxg:GridColumn FieldName="WallLocation" AllowEditing="False"/>
            <dxg:GridColumn FieldName="SquareFoot" AllowEditing="False"/>
            <dxg:GridColumn FieldName="Weight" AllowEditing="False"/>
            <dxg:GridColumn FieldName="UnitCost" AllowEditing="False"/>
            <dxg:GridColumn FieldName="TotalCost" AllowEditing="False"/>
            <dxg:GridColumn FieldName="FuelSurcharge" AllowEditing="False"/>

        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView ShowTotalSummary="True" AutoWidth="True" DetailHeaderContent="True"  ShowIndicator="False" ShowGroupPanel="False"><!--GroupRowTemplate="{StaticResource descriptionHeader}"-->
            </dxg:TableView>
        </dxg:GridControl.View>
    </dxg:GridControl>




protected void GetAllInfills()
        {
            List<Infill> infillList = new List<Infill>();
            infillList=BLL.GetAllInfills();
            if (infillList != null)
            {
                grdInfill.ItemsSource = infillList;

                grdInfill.GroupBy(grdInfill.Columns["GlassType"], ColumnSortOrder.Ascending);
                grdInfill.GroupBy(grdInfill.Columns["GlassDescription"], ColumnSortOrder.Ascending);

                grdInfill.AutoExpandAllGroups = true;

            }
        }

从上面marukup我想访问它包含网格列'GlassDescription组头部分价值TextBlock的控制即 txtdescription 现在我想这个值INT分成三个值,即txtdescription.Split(*)并分配在DataTemplate中从背后代码值三个其他文本块即txtdesc1,txtdesc2,txtdesc3。

From Above marukup i want to access the TextBlock Control i.e 'txtdescription' which contains the group header section value of Grid Column 'GlassDescription' now i want to split this value int to three value i.e txtdescription.Split('*') and assign values to other three textblock i.e txtdesc1,txtdesc2,txtdesc3 that are in DataTemplate from code behind.

推荐答案

由于您请求的样本中,我使用列表框提供样品。结果
<击>
XAML

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>



代码隐藏

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; }
}



编辑回应其他信息

在WPF中你可以使用元素绑定,它允许你访问一个特定元素的任何财产。既然你要访问txtdescription文本块的Text属性,你将不得不使用的元件结合。但是你wan't到一分为三的TextBlocks。所以,你会需要一个转换器。

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.

使用下面的代码元件结合

<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>



这里是转换器代码

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



最后包括转换器命名空间在XAML

<Window x:Class="WpfApplication1.DataTemplateWindow"
    xmlns:cv="clr-yourconverterclassnamespace"
    ...
    >
<Window.Resources>
     <cv:SplitterConverter x:Key="splitter" />
</Window.Resources>
....
</Window>

这篇关于如何找到一个控件,里面的DataTemplate&安培;在WPF赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆