如何为绑定到 ItemControl 中的标签的字符串对象 属性 的部分获取不同的颜色?
How do I get different colors for parts of string object property binded to label in 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>
用户控件绑定到一个对象,该对象具有类型为 selectedLevel 的可观察集合(如顶部所示)。
我正在视图模型中添加 SelectedLevels 的属性,如下所示:
selectedLevels.Add(new selectedLevel
{
Level = "Mahadasha",
PlanetSelected = Mahadashas[0].rulerName,
houseDetails = indicesList[0] + ", " + indicesList[1] + ", " + indicesList[2]
});
我试图在与 houseDetails
绑定的第 3 个标签中为 indicesList[0]
、indicesList[1]
和 indicesList[2]
设置不同的颜色,但我只能更改全文颜色而不是它的一部分。
我完全迷失在这里。关于如何为字符串的各个部分设置不同的颜色,我可以获得一些帮助吗?
很简单,首先你要把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]
});
这是我的对象类型:
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>
用户控件绑定到一个对象,该对象具有类型为 selectedLevel 的可观察集合(如顶部所示)。
我正在视图模型中添加 SelectedLevels 的属性,如下所示:
selectedLevels.Add(new selectedLevel
{
Level = "Mahadasha",
PlanetSelected = Mahadashas[0].rulerName,
houseDetails = indicesList[0] + ", " + indicesList[1] + ", " + indicesList[2]
});
我试图在与 houseDetails
绑定的第 3 个标签中为 indicesList[0]
、indicesList[1]
和 indicesList[2]
设置不同的颜色,但我只能更改全文颜色而不是它的一部分。
我完全迷失在这里。关于如何为字符串的各个部分设置不同的颜色,我可以获得一些帮助吗?
很简单,首先你要把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]
});