问题描述
我如何访问ListBox&中的数据模板内的文本块?设置文本块的"MaxHeight"?
how do i access a textblock inside a data template, within a ListBox & set the textblock's 'MaxHeight'?
还有代码隐藏吗?
我有@ Page.xaml:
I have @ Page.xaml:
<ListBox ItemsSource="{Binding}" DataContext="" x:Name="NewsList" SelectionChanged="NewsList_SelectionChanged" SelectionMode="Single" Width="580" Height="360" VerticalAlignment="Top" HorizontalAlignment="Center" >
<ListBox.ItemTemplate>
<DataTemplate>
<!-- table within each ListBox item-->
<!-- control horizontal scrollbar w width @ following line-->
<Grid Height="110" Width="540">
<Grid.RowDefinitions>
<RowDefinition Height="15" />
<RowDefinition Height="*" />
<RowDefinition Height="15" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="82" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border CornerRadius="2" BorderThickness="2" BorderBrush="Gray" Height="82" Width="82" Background="LemonChiffon" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.RowSpan="3" Grid.Column="0" >
<Image Source="{Binding NewsThumbnail}" Style="{StaticResource ThumbNailPreview}" />
</Border>
<TextBlock x:Name="NewsTitle" FontFamily="Arial" FontWeight="bold" TextDecorations="Underline" Text="{Binding Title}" Style="{StaticResource TitleText}" Grid.Row="0" Grid.Column="1"/>
<!-- i wish to set the 'maxheight' of the following textblock instead of a fixed value of 65 -->
<TextBlock x:Name="StaffNewsBody" FontFamily="Arial" Text="{Binding NewsBody}" Style="{StaticResource DescriptionBlock}" Grid.Row="1" Grid.Column="1" MaxHeight="65" />
@ Page.xaml.cs:
@ Page.xaml.cs:
//variable values taken from App.xaml.cs's method 'Application_Startup()'
public Page(string _setArticles, string _setLength)
{
InitializeComponent();
if (!string.IsNullOrEmpty(_setLength))
{
// if (_setLength.Contains("_3"))
// if (_setLength.Contains("_4"))
// if (_setLength.Contains("_5"))
}
}
推荐答案
互联网上有几本关于如何访问数据模板内部控件的教程.您可以在Google上搜索它,找到类似此页面的页面和此.找到所需的控件后,您可以按所需的任何方式对其进行操作,并且可以应用诸如最大高度"之类的任何更改
There are several tutorials on the internet about how to access a control inside data template. You can google it you will find pages like this and this. Once you have located the control you want, you can play with it in any way you want and you can apply any change like Maximum Height
更新:参见此,它是访问DataTemplate中的控件的最简单的方法
Update:See this, it is the simplest possible way to access a control inside DataTemplate
这篇关于设置嵌入在ListBox数据模板中的textblock的MaxHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!