【问题标题】:Access and modify properties of grid row within data template访问和修改数据模板中网格行的属性
【发布时间】:2011-07-29 02:12:32
【问题描述】:

尽管定义了网格或行的名称,但我难以访问网格或其行(在 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);
        }

【问题讨论】:

    标签: c# xaml silverlight-2.0


    【解决方案1】:

    我相信 StaffNews 代表您列表中的一项,对吗?

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

    test.StaffNewsBodyHeight = 200;
    

    替换你的 sn-p 的最后一行。

    [附注- StaffNewsBodyHeight 是 StaffNews 中定义的依赖属性,对吧?]

    【讨论】:

    • 非常感谢。找到了一个有效的解决方法。非常感激! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2013-12-12
    • 2011-03-02
    相关资源
    最近更新 更多