[英]change foreground color of textblock inside stackpanel using c# in windows phone
這是我的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;
但是它不認識它,那么如何實現呢? 還有沒有其他方法可以使用綁定更改前景色
嘗試這個:
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);
正如我在評論中說的那樣,您的代碼未編譯的原因是因為您的TextBlock
是ListBox
的DataTemplate
的一部分,而不是頁面本身上的對象。 因此,您希望存在多個實例。 您如何僅用一個標識符就能識別它們呢?
您稍后在注釋中提到,您要做的是根據項目的屬性值更改每個列表項目中TextBlock
的顏色。 有幾種方法可以做到這一點。
您可以使用Blend行為:
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.