[英]How i get the control inside dataTemplate in windows phone?
我想更改每個listboxitem增加的邊框的顏色os背景。
<ListBox.ItemTemplate>
<DataTemplate>
<border x:name: border>
<ListBoxItem ItemSource={Binding Example}>
</ListBoxItem>
</border>
有任何想法嗎?
首先檢查此鏈接 ,了解如何使用轉換器。
然后在您的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)
{
}
}
因此,此代碼動態返回顏色之一。 並且有機會連續獲得相同的顏色。 順便說一句,我沒有測試上面的代碼。
“ 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.