Установить MaxHeight текстового блока, встроенного в шаблон данных ListBox - PullRequest
0 голосов
/ 30 июля 2011

как мне получить доступ к текстовому блоку внутри шаблона данных, внутри ListBox и установить MaxHeight для текстового блока?

Кодовый код также?

У меня есть @ 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:

//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"))
    }      
}    

1 Ответ

0 голосов
/ 30 июля 2011

В Интернете есть несколько учебных пособий о том, как получить доступ к элементу управления внутри шаблона данных. Вы можете Google, вы найдете такие страницы, как это и это . После того, как вы нашли нужный элемент управления, вы можете играть с ним любым удобным для вас способом и можете применить любое изменение, например, Максимальная высота

Обновление: См. this , это самый простой способ получить доступ к элементу управления внутри DataTemplate

...