[英]Write Xaml Textblock in C#
我正在使用以下代碼將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”中。
我該怎么做呢?
一種實現方法是,在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頁面的代碼隱藏之外訪問“ price” Paragraph元素,則需要將Paragraph元素的聲明更改為以下內容:
<Paragraph x:Name="price" x:FieldModifier="public" />
最后要注意的一點是,如果命名元素位於DataTemplate中(通常與WPF項目控件一起使用),則無法訪問它們。 因此,如果是這種情況,則可能要使用MVVM方法。
為什么不放置Grid.Column =“”和Grid.Row =“”
<Paragraph Grid.Column="0" and Grid.Row="0">
</Paragraph>
我鼓勵您使用MVVM並嘗試以下操作:
<RichTextBlock FontSize="12" FontFamily="Segoe UI" >
<Paragraph >
<Run Text="{Binding Sale.Text}"/> <Run Text="{Binding Sale.Text }"/>
</Paragraph>
</RichTextBlock>
如果不使用MVVM,則必須創建“運行”對象,插入文本,然后將其添加到RichTextBlock的文本中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.