【问题标题】:Write Xaml Textblock in C#用 C# 编写 Xaml 文本块
【发布时间】:2013-11-11 14:10:43
【问题描述】:

我正在使用此代码将 Textblock 写入 Xaml 页面:

  Sale.Name = "Price";
  Sale.Text = "$200";
  deatilLayout.Children.Add(Sale);
  Grid.SetRow(Sale, 1);

这成功地将数据放置在 xaml 页面上,但位置不正确。

  <FlipView
            x:Name="flipView"
            AutomationProperties.AutomationId="ItemsFlipView"
            AutomationProperties.Name="Item Details"
            TabIndex="1"
            Grid.RowSpan="2"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}">

            <FlipView.ItemContainerStyle>
                <Style TargetType="FlipViewItem">
                    <Setter Property="Margin" Value="0,137,0,70"/>
                </Style>
            </FlipView.ItemContainerStyle>

            <FlipView.ItemTemplate>
                <DataTemplate>

                    <!--
                        UserControl chosen as the templated item because it supports visual state management
                        Loaded/unloaded events explicitly subscribe to view state updates from the page
                    -->
                    <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                        <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">

                            <!-- Content is allowed to flow across as many columns as needed -->
                            <common:RichTextColumns x:Name="richTextColumns" Margin="117,0,117,47">
                                <RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}" IsTextSelectionEnabled="False">

                                    <Paragraph LineStackingStrategy="MaxHeight" Margin="0,0,0,180">
                                        <InlineUIContainer>
                                            <Image x:Name="image" MaxHeight="480" Margin="0,20,0,100" Stretch="Uniform" Source="{Binding Image}" AutomationProperties.Name="{Binding Title}"/>
                                        </InlineUIContainer>
                                    </Paragraph>
                                    <Paragraph Margin="0,0,0,30" LineHeight="65" FontFamily="Global User Interface">
                                        <Run FontSize="52"  FontWeight="Thin" Text="{Binding Title}" />

                                    </Paragraph>
                                    <Paragraph Margin="0,0,0,50">
                                        <Run Text="TT $" FontSize="24" FontWeight="Thin"/>
                                        <Run FontSize="30" FontWeight="Normal" Text="{Binding Subtitle}"/>


                                    </Paragraph>
                                    <Paragraph x:Name="price" x:FieldModifier="public" />
                                    <Paragraph Margin="0,0,0,40">

                                        <Run FontSize="27" FontWeight="Normal" Text="{Binding Weight}"/>

                                    </Paragraph>
                                    <Paragraph Margin="0,0,0,40" LineHeight="40">

                                        <Run FontSize="35" FontWeight="Normal" Text="{Binding Description}"/>

                                    </Paragraph>
                                    <Paragraph Margin="0,0,0,40" LineHeight="30">

                                        <!--<Run FontSize="22" FontWeight="SemiLight" Text="{Binding Category}"/>-->
                                    </Paragraph>
                                </RichTextBlock>

                                <!-- Additional columns are created from this template -->
                                <common:RichTextColumns.ColumnTemplate>
                                    <DataTemplate>
                                        <RichTextBlockOverflow Width="560" Margin="30,0,0,0">
                                            <RichTextBlockOverflow.RenderTransform>
                                                <TranslateTransform X="-1" Y="4"/>
                                            </RichTextBlockOverflow.RenderTransform>
                                        </RichTextBlockOverflow>
                                    </DataTemplate>
                                </common:RichTextColumns.ColumnTemplate>
                            </common:RichTextColumns>

                            <VisualStateManager.VisualStateGroups>

                                <!-- Visual states reflect the application's view state inside the FlipView -->
                                <VisualStateGroup x:Name="ApplicationViewStates">
                                    <VisualState x:Name="FullScreenLandscape"/>
                                    <VisualState x:Name="Filled"/>

                                    <!-- Respect the narrower 100-pixel margin convention for portrait -->
                                    <VisualState x:Name="FullScreenPortrait">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="richTextColumns" Storyboard.TargetProperty="Margin">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="97,0,87,57"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="MaxHeight">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="400"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>

                                    <!-- When snapped, the content is reformatted and scrolls vertically -->
                                    <VisualState x:Name="Snapped">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="richTextColumns" Storyboard.TargetProperty="Margin">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="17,0,17,57"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="scrollViewer" Storyboard.TargetProperty="Style">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource VerticalScrollViewerStyle}"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="richTextBlock" Storyboard.TargetProperty="Width">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="280"/>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="image" Storyboard.TargetProperty="MaxHeight">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="160"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </ScrollViewer>
                    </UserControl>
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>

我想将段落中的数据放在RichTextBlock中,即Grid“detailLayout”中。

我该怎么做?

【问题讨论】:

    标签: c# xaml visual-studio-2012 windows-8


    【解决方案1】:

    一种方法是,在 XAML 文件中,将 Paragraph 元素更改为以下内容:

    <Paragraph x:Name="price" />
    

    这使得 Paragraph 实例作为名称为“price”的字段对代码隐藏可见。然后,例如,您可以使用以下代码在代码隐藏中访问和设置此 Paragraph 实例的内容:

    string textToSet = "Price: $200"; // Replace this value with whatever value you want to 
                                      // set in the paragraph.
    Run textRun = new Run();
    textRun.Text = textToSet;    
    this.price.Inlines.Add(textRun);
    

    那么,如果要清除Paragraph实例的内容,可以在代码隐藏中进行如下操作:

    this.price.Inlines.Clear();
    

    默认情况下,在为 XAML 页面中的元素分配名称时,该元素对外部代码不公开可见。如果需要从 XAML 页面的代码隐藏之外访问“价格”Paragraph 元素,则需要将 Paragraph 元素的声明更改为以下内容:

    <Paragraph x:Name="price" x:FieldModifier="public" />
    

    最后要注意的是,如果命名元素位于 DataTemplate 中(通常与 WPF 项控件一起使用),您将无法访问它们。因此,如果是这种情况,您可能需要采用 MVVM 方法。

    【讨论】:

    • 代码无法识别price
    • 我进行了编辑,以进一步阐明需要做什么才能使“价格”元素可以从 XAML 页面范围之外访问。虽然我不确定这是否会解决您的问题,因为我还没有看到您的 C# 代码相对于您的 XAML 页面在范围方面的位置。
    • 我仍然收到错误Error 1 'BG.ItemDetailPage' does not contain a definition for 'price' and no extension method 'price' accepting a first argument of type 'BG.ItemDetailPage' could be found (are you missing a using directive or an assembly reference?) C# 是 Xaml 页面的文件。此 Xaml 页面是 ItemDetailPage.xaml,带有 C# 代码的文件是 ItemDetailPage.xaml.cs
    • 你确定 ItemDetailPage.xaml 有一个类似下面的元素:&lt;Paragraph x:Name="price" /&gt;
    • 如果没有看到相关代码的更完整视图,恐怕我将无法诊断您的问题。
    【解决方案2】:

    为什么不放置 Grid.Column="" 和 Grid.Row=""

    <Paragraph Grid.Column="0" and Grid.Row="0">
    </Paragraph>
    

    【讨论】:

      【解决方案3】:

      我鼓励您使用 MVVM 并尝试以下操作:

       <RichTextBlock    FontSize="12"    FontFamily="Segoe UI" >
                                      <Paragraph >
                                          <Run Text="{Binding Sale.Text}"/> <Run Text="{Binding Sale.Text }"/>
                                      </Paragraph>
                                  </RichTextBlock>
      

      如果您不使用 MVVM,则必须创建一个“运行”对象,插入文本,并将其添加到 RichTextBlock 的子项中。

      【讨论】:

        猜你喜欢
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多