简体   繁体   中英

WPF TabItem Listbox dynamic binding

Hy,

I would like to bind a color property to the background of a tab content:

<CollectionViewSource x:Key="MyCollectionViewSource" Source="{Binding QuickDialColl, Source={x:Static vm:QuickDialViewModel.Instance}}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Category" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

The QuickDialColl is an ObservableCollection of QuickDial Objects:

    class QuickDial
{
    public string Name { get; set; }
    public string ToolTip { get; set; }
    public string Number { get; set; }
    public string Category { get; set; }
    public string CustomBackgroundColor { get; set; }
    public string TextColor { get; set; }

    public QuickDial()
    {
    }
}

<TabControl ItemsSource="{Binding Groups, Source={StaticResource MyCollectionViewSource}}" SelectedIndex="0" >

    <TabControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </TabControl.ItemTemplate>

    <TabControl.ContentTemplate>
        <DataTemplate>
            <ListBox ItemsSource="{Binding Items}" 
                IsManipulationEnabled="False" 
                ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                HorizontalContentAlignment="Stretch" 
                VerticalContentAlignment="Stretch">

                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding Name}" 
                            Background="{Binding CustomBackgroundColor,Converter={StaticResource HexToColorConverter}}"
                            ToolTip="{Binding ToolTip}"  
                            Width="130"
                            Margin="2,2,2,2" 
                             />

                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DataTemplate>
    </TabControl.ContentTemplate>

</TabControl>

The dynamic binding is ok for the buttons with:

Background="{Binding CustomBackgroundColor,Converter={StaticResource HexToColorConverter}}"

But I wish it works for the background of the tab contentpresenter (as all the buttons of the same category have the same background color).

I have tried to bind the wrappanel and the listbox bacground property with the same piece of code but it didn't work.

Here is the result I would like to get:

Tab Content background

I am surely missing the point...

Many thanks for you help. Chris.

Your converter should return a SolidColorBrush instead of color

public class HexToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return new SolidColorBrush(Colors.Blue);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM