【问题标题】:Set MaxHeight of textblock embedded within data template of ListBox设置嵌入在 ListBox 数据模板中的文本块的 MaxHeight
【发布时间】:2011-07-30 07:46:10
【问题描述】:

如何访问数据模板内的文本块,在 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"))
    }      
}    

【问题讨论】:

    标签: c# datatemplate silverlight-2.0


    【解决方案1】:

    互联网上有几个关于如何访问数据模板中的控件的教程。你可以谷歌它,你会发现像thisthis这样的页面。一旦你找到了你想要的控件,你可以用任何你想要的方式来玩它,你可以应用任何改变,比如最大高度

    更新: 请参阅this,这是访问 DataTemplate 中的控件的最简单方法

    【讨论】:

    • 谢谢 :) 有你知道的更简单的例子吗?它们与我过去 3 天遇到的相似。请指导我?
    • 非常感谢! :) 会试试看。
    • 好的哇!它对我来说是一个临时解决方案,但在那之前,我的问题少了 1 个。在那之前,再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2011-06-14
    • 1970-01-01
    • 2012-11-09
    相关资源
    最近更新 更多