I do not know how your RoleList is defined.
Maybe this example can lead you into the right direction:
public class Role
{
public string Name { get; set; }
public string Type { get; set; }
}
And your ObservableCollection is defined like this
private ObservableCollection<Role> roleList;
public ObservableCollection<Role> RoleList
{
get { return roleList; }
set
{
if (roleList == value) return;
roleList = value;
OnPropertyChanged();
}
}
Then you could display your collection in a ListBox.
You must use a DataTemplate.
<ListBox ItemsSource="{Binding RoleList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding Type}"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
A TextBlock alone cannot display a collection.
A better idea would be to use a DataGrid.
<DataGrid ItemsSource="{Binding RoleList}"
AutoGenerateColumns="False"
HeadersVisibility="Column"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Name"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Type"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Type}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
If you want to have something like a news ticker you need a value converter
for your original canvas solution.
namespace WpfApplicationTest1.Converters
{
public class ListConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var roles = value as ObservableCollection<Role>;
if (roles != null)
{
var stringBuilder = new StringBuilder();
foreach (var role in roles)
{
stringBuilder.Append($"{role.Name} {role.Type}");
}
return stringBuilder.ToString();
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
In your view you must use the value converter like this.
Add this to your Window namespace section:
xmlns:local="clr-namespace:WpfApplicationTest1"
<TextBlock Text="{Binding RoleList, Converter={StaticResource ListConverter}}" FontSize="25" Name="tbmarquee" Canvas.Top="25" Width="1346"
RenderTransformOrigin="0.465,0.524" />