【问题标题】:How i get the control inside dataTemplate in windows phone?我如何在 windows phone 中的 dataTemplate 中获得控件?
【发布时间】:2012-11-21 11:38:30
【问题描述】:

我想改变每个listboxitem增加的边框的颜色os背景。

 <ListBox.ItemTemplate>
            <DataTemplate>
                <border x:name: border>
                   <ListBoxItem ItemSource={Binding Example}>
                   </ListBoxItem>
                </border>

有什么想法吗?

【问题讨论】:

    标签: windows windows-phone-7 listboxitem


    【解决方案1】:

    首先查看this link,了解如何使用转换器。

    然后在你的 XAML 中,像这样写你的边框

    <Border BorderBrush="{Binding Converter=ColorConverter}">
     ....
    <Border>
    

    将您的转换器代码修改为类似的内容

    public class ColorConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Define some random colors
        Color[] colors = { Colors.Blue, Colors.Brown, Colors.Cyan, Colors.Green, Colors.Magenta, Colors.Orange, Colors.Purple, Colors.Yellow, Colors.LightGray };
    
        return colors[(new Random()).Next(8)];
    }
    
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
    
    }
    }
    

    因此,此代码会动态返回其中一种颜色。并且有机会连续获得相同的颜色。顺便说一句,我没有测试上面的代码。

    【讨论】:

      【解决方案2】:

      “IValueConverter”的 TypeConverter 不支持转换

      当我像上面那样放置边框时会导致此错误。

      这是我的完整代码

      <ItemsControl x:Name="listaAdd" ItemsSource="{Binding sample}"  Grid.Row="0" Margin="0,221,0,0" Foreground="White" Background="#FF5B5B5B" >
              <ItemsControl.ItemTemplate>
                  <DataTemplate>
                      <Grid HorizontalAlignment="Stretch" Grid.Row="1" Width="480" >
                          <Border x:Name="borda" BorderBrush="{Binding Converter=ColorConverter}"  >
                              <ListBoxItem x:Name="listSelected" Foreground="White" IsSelected="True"  VerticalAlignment="Center"  FontSize="{StaticResource PhoneFontSizeLarge}" Content="{Binding Nome}"  Background="{x:Null}" HorizontalContentAlignment="Left" Height="80" DoubleTap="listSelected_DoubleTap">
                              </ListBoxItem>
                          </Border>
                          <toolkit:ContextMenuService.ContextMenu>
                              <toolkit:ContextMenu x:Name="subMenulist">
                                  <Button Grid.Column="1" Content="delete"   BorderThickness="0" Margin="0"  Background="White" Foreground="#FF1A739D" FontSize="32" HorizontalContentAlignment="Left">
                                  </Button>
                                  <Button Grid.Column="1" Content="compartilhar" Margin="0,0,0,0" x:Name="btnShare" BorderThickness="0"  Background="White" Foreground="#FF1A739D" FontSize="32" HorizontalContentAlignment="Left">
                                  </Button>
                              </toolkit:ContextMenu>
                          </toolkit:ContextMenuService.ContextMenu>
                      </Grid>
                  </DataTemplate>
              </ItemsControl.ItemTemplate>
          </ItemsControl>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-27
        相关资源
        最近更新 更多