这是我的xaml
<ListBox Margin="3,60,1,10" BorderThickness="2" Grid.Row="1" Name="lstAnnouncement" Tap="lstAnnouncement_Tap" Width="476" d:LayoutOverrides="VerticalMargin">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="thispanel" Grid.Row="1" Orientation="Horizontal" Height="120" Width="478" >
<StackPanel.Background>
<ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
<!--<SolidColorBrush Color="{Binding Path=background}"/>-->
</StackPanel.Background>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Delete" Click="MenuItem_Click"/>
<!--<toolkit:MenuItem Header="Remove " Click="MenuItem_Click"/>-->
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<Grid HorizontalAlignment="Left" Width="30" Margin="0,0,0,2" Background="#FF0195D5" Height="118">
<!--<Grid.Background>
<ImageBrush ImageSource="Images/Text-ALU.png" Stretch="Fill" />
</Grid.Background>-->
<TextBlock x:Name="txtDate" TextWrapping="Wrap" Text="{Binding Path=announcementDate}" RenderTransformOrigin="0.5,0.5" Margin="-43.169,44.001,-43.831,0" UseLayoutRounding="False" d:LayoutRounding="Auto" TextAlignment="Center" Height="30" VerticalAlignment="Top" Width="117">
<TextBlock.RenderTransform>
<CompositeTransform Rotation="-90"/>
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
<Grid HorizontalAlignment="Left" Width="5" Height="120"/>
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Width="432" Height="114">
<TextBlock x:Name="txtTitle" Height="27" TextWrapping="Wrap" Text="{Binding Path=announcementTitle}" FontSize="18.667" HorizontalAlignment="Left" Width="432" FontWeight="Bold" />
<StackPanel Orientation="Horizontal" Width="432" Height="27">
<TextBlock x:Name="txtBy" FontWeight="Bold" Height="27" TextWrapping="Wrap" Text="{Binding Path=announcementBy}" FontSize="18.667" Width="399"/>
<Image x:Name="imgArrow" Width="25" Source="Images/Go-In-Arrow.png" Height="25" Margin="5,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Width="433" Height="60">
<TextBlock x:Name="txtDesc" FontWeight="Bold" Height="58" TextWrapping="Wrap" Text="{Binding Path=announcementShortDescription}" FontSize="18.667" Width="371"/>
<TextBlock x:Name="txtID" Height="56" Text="{Binding Path=announcementID}" TextWrapping="Wrap" Foreground="Black" FontSize="18.667" Width="8" Visibility="Collapsed"/>
<Image x:Name="imgType" Width="35" Source="{Binding Path=announcementTypeImage}" Height="40" Margin="27,20,0,0" d:LayoutOverrides="Height"/>
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我想使用c#
在代码后面用x:name txtDesc更改textblocxk的前景色我正在尝试
txtDesc.Foreground = Brushes.White;
但它没有认识到它如何实现它?还有其他方法可以使用绑定
来更改前景色答案 0 :(得分:1)
试试这个:
的Xaml:
<TextBlock x:Name="txtDesc" FontWeight="Bold" Height="58" TextWrapping="Wrap" Text="{Binding Path=announcementShortDescription}" FontSize="18.667" Width="371" Foreground="Yellow"/>
CS:
txtDesc.Foreground = new SolidColorBrush(Colors.Yellow);
答案 1 :(得分:1)
正如我在评论中所说,代码未编译的原因是因为TextBlock
是DataTemplate
ListBox
的一部分,而不是页面本身的对象。因此,您希望存在多个实例;你怎么能用一个标识符来识别它们呢?
您稍后在评论中提到,您要做的是根据项目属性的值更改每个列表项中TextBlock
的颜色。有几种方法可以做到。
您可以使用混合行为:
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding announcementShortDescription}" >
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Value="1" Binding="{Binding itemread}">
<Core:ChangePropertyAction PropertyName="Foreground">
<Core:ChangePropertyAction.Value>
<SolidColorBrush Color="Blue"/>
</Core:ChangePropertyAction.Value>
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
我首选的选择是使用值转换器。类似的东西:
class ItemReadToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
int val = (int)value;
return val == 0 ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.Blue);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
在XAML中:
<ListBox x:Name="list">
<ListBox.Resources>
<local:ItemReadToColorConverter x:Key="conv" />
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Foreground="{Binding itemread, Converter={StaticResource conv}}" Text="{Binding announcementShortDescription}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>