访问和修改数据模板中网格行的属性

时间:2011-07-29 02:12:32

标签: c# xaml silverlight-2.0

我很难访问网格或其行(在ListBox中),尽管定义了网格或行的名称。当一些参数从SharePoint可浏览属性传递到Silverlight控件时,我打算将一个新的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 >                                       
                                                                                                <Grid Height="110" Width="540" x:Name="StaffNewsBodyHeight">
                                                     <Grid Height="110" Width="540">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="15" />
                                                    <RowDefinition Height="{Binding StaffNewsBodyHeight}" />
                                                    <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"/>                                         
                          <TextBlock x:Name="NewsBody" FontFamily="Arial" Text="{Binding NewsBody}" Margin="5" Style="{StaticResource DescriptionBlock}" Grid.Row="1" Grid.Column="1" />



                                                    <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1" Margin="3,3,3,3">
                                                        <TextBlock Text="Published By:" HorizontalAlignment="Left" VerticalAlignment="Bottom" Foreground="Gray" FontWeight="Bold" FontSize="9" /> 
                                                        <TextBlock Text="{Binding PublishedBy}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Foreground="Gray" FontWeight="Bold" FontSize="9" />
                                                        <TextBlock Text="{Binding DatePublished}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Foreground="Gray" FontWeight="Bold" FontSize="9" />                                                    
                                                    </StackPanel>                                                
                                                </Grid>                                       
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>

@ Page.xaml.cs:

if (!string.IsNullOrEmpty(_setLength))
        {               
            StaffNews test = new StaffNews();

            //assign new height to gridrow of NewsBody
            if (_setLength.Contains("_3"))
                test.StaffNewsBodyHeight.Equals(200);
        }

1 个答案:

答案 0 :(得分:0)

我相信StaffNews代表你名单中的一个项目,对吗?

在这种情况下,要为属性指定值,请使用:

test.StaffNewsBodyHeight = 200;

替换代码段的最后一行。

[P.S。 - StaffNewsBodyHeight是StaffNews中定义的依赖属性,对吗?]