0

我有一个绑定到网格组标题部分的 DataTemplate。DataTemplate 中有四个 TextBlock,其中一个 TextBlock 包含 Grid Header Column Value。现在,我想将此 TextBlock 值拆分为三个,并将此值分配给 Code Behind 中的其他三个 TextBlock。可能吗?

<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 我想访问 TextBlock 控件,即“ txtdescription”,它包含 Grid Column 的组标题部分值,'GlassDescription'现在我想将此值 int 拆分为三个值,即 txtdescription.Split('*') 并将值分配给其他三个文本块即 txtdesc1,txtdesc2,txtdesc3 从后面的代码中位于 DataTemplate 中。

4

1 回答 1

1

由于您要求提供示例,因此我提供了使用 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 文本块的文本属性,因此您必须使用元素绑定。但是您不想将其拆分为三个 TextBlock。所以你需要一个转换器。

使用下面的代码进行元素绑定

<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>
于 2013-05-24T06:15:31.127 回答