如何为绑定到 ItemControl 中的标签的字符串对象属性的部分获取不同的颜色?

问题描述

这是我的对象类型:

public class selectedLevel
{
   public string Level
    {
        get;
        set;
    }

   public string PlanetSelected
    {
        get; set;

    }
   public string houseDetails
    {
        get;
        set;
    }

    
}

这是我的项目模板:


<ItemsControl x:Name="LevelDetails" Margin="-464,416,120,-484"   BorderBrush ="Black"  ItemsSource="{Binding selectedLevels}" Grid.ColumnSpan="3" HorizontalContentAlignment="Stretch">
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <Grid>
            <Grid.ColumnDeFinitions>
               <ColumnDeFinition SharedSizeGroup="Col1" />
               <ColumnDeFinition SharedSizeGroup="Col2" />
               <ColumnDeFinition SharedSizeGroup="Col3" />
               <ColumnDeFinition SharedSizeGroup="Col4" />
               <ColumnDeFinition SharedSizeGroup="Col5" />
               <ColumnDeFinition SharedSizeGroup="Col6" />
               <ColumnDeFinition SharedSizeGroup="Col7" />
               <ColumnDeFinition SharedSizeGroup="Col8" />
               <ColumnDeFinition SharedSizeGroup="Col9" />
               <ColumnDeFinition SharedSizeGroup="Col10"/>
            </Grid.ColumnDeFinitions>
            <Grid.RowDeFinitions>
               <RowDeFinition SharedSizeGroup="Row1"/>
               <RowDeFinition SharedSizeGroup="Row2"/>
               <RowDeFinition SharedSizeGroup="Row3"/>
            </Grid.RowDeFinitions>
            <Label x:Name="LevelName" Grid.Row="0"  Content="{Binding Level}" HorizontalContentAlignment="Center" FontWeight="Bold" FontSize="16" BorderBrush="Black" BorderThickness="1" Background="{x:Null}"/>
            <Label x:Name="PlanetName" Grid.Row="1" Content="{Binding PlanetSelected}" FontSize="14" Foreground="Red" BorderBrush="Black" FontWeight="Bold" HorizontalContentAlignment="Center" BorderThickness="1" />
            <Label x:Name="LevelDetails" Grid.Row="2" HorizontalContentAlignment="Center" Content="{Binding houseDetails}" FontSize="14" BorderBrush="Black" FontWeight="Bold" BorderThickness="1" />
         </Grid>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

User 控件绑定到一个对象,该对象具有 selectedLevel 类型的可观察集合(显示在顶部)。

我在视图模型中添加 SelectedLevels 的属性如下:

selectedLevels.Add(new selectedLevel
{
    Level = "Mahadasha",PlanetSelected = Mahadashas[0].rulerName,houseDetails =  indicesList[0]  +  "," + indicesList[1] + "," + indicesList[2]
                    
});
       

我试图在与 indicesList[0] 绑定的第三个标签中为 indicesList[1]indicesList[2]houseDetails 获取不同的颜色,但我只能更改全文颜色而不能一部分。

我完全迷失在这里。我可以获得有关如何为字符串的各个部分设置不同颜色的帮助吗?

解决方法

很简单,首先需要将 houseDetails 属性分成 3 个字符串,类似这样:

public class selectedLevel
{
   public string Level { get; set; }

   public string PlanetSelected { get; set; }

   public string houseDetails1 { get; set; }
   public string houseDetails2 { get; set; }
   public string houseDetails3 { get; set; }
}

然后您可以使用由 TextBlock 包裹的 Border 而不是第三个 Label 并在其中使用 Runs 为每个选择不同的颜色并绑定它们到相应的属性:

<Border HorizontalAlignment="Center" BorderBrush="Black" BorderThickness="1">
    <TextBlock x:Name="LevelDetails" Grid.Row="2" FontSize="14" FontWeight="Bold">
        <Run Text="{Binding houseDetails1}" Foreground="Red"/>,<Run Text="{Binding houseDetails2}" Foreground="Green"/>,<Run Text="{Binding houseDetails3}" Foreground="Blue"/>
    </TextBlock>
</Border>

然后在视图模型中:

selectedLevels.Add(new selectedLevel
{
    Level = "Mahadasha",PlanetSelected = Mahadashas[0].rulerName,houseDetails1 =  indicesList[0],houseDetails2 =  indicesList[1],houseDetails3 =  indicesList[2]                   
});