बुला यहाँ मुझे लगता है कि के साथ आया था प्रणाली रंग या नियंत्रण टेम्पलेट्स बदलते को शामिल नहीं करता है से पहले
FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = false;
जोड़ रहा है। बस एक नए उपयोगकर्ता नियंत्रण में ListBox लपेटें।
public partial class StyledListBox : UserControl
{
public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
}
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public StyledListBox()
{
InitializeComponent();
}
public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(StyledListBox), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(StyledListBox), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(StyledListBox), new FrameworkPropertyMetadata(null)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
}
XAML:
<UserControl x:Class="StyledListBox"
<ListBox ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}"
SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<ContentPresenter ContentTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StyledListBox}}}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
तब बस अपने आवरण UserControl का उपयोग के रूप में अगर यह एक ListBox थे। आप जिस भी अन्य सूची बॉक्स गुणों को नियंत्रित करना चाहते हैं, उसे मेरे उदाहरण से ItemsSource
और SelectedItem
जैसे ही रैपर में जोड़ा जा सकता है।
[सूची/कॉम्बो बॉक्स पृष्ठभूमि और चयनित रंगों के तहत चयनित 4.5 के संभावित डुप्लिकेट] (http://stackoverflow.com/questions/12007918/list-combo-box-background-and-selected-colours-under-net -4-5) –