C# 在ListboxItem中绑定BorderBackground

C# 在ListboxItem中绑定BorderBackground,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我尝试了不同的方法来解决这个问题。首先,我在ViewModel中将主颜色绑定为字符串、颜色、画笔,并尝试将其绑定到边框->不适用于我。第二个是转换器,但我无法绑定正确的字符串来激活转换器。唯一可以将MainColor作为字符串绑定到标签样式的方法,也是我在同一资源中使用的方法。因此,现在标签中的更改有效,但列表框中的更改无效 我的看法是: ResourceDictionary中的我的列表框: 主颜色在边框x:Name=original中不起作用 我的标签: 这里的主色很好用 我的ViewMode

我尝试了不同的方法来解决这个问题。首先,我在ViewModel中将主颜色绑定为字符串、颜色、画笔,并尝试将其绑定到边框->不适用于我。第二个是转换器,但我无法绑定正确的字符串来激活转换器。唯一可以将MainColor作为字符串绑定到标签样式的方法,也是我在同一资源中使用的方法。因此,现在标签中的更改有效,但列表框中的更改无效

我的看法是:

ResourceDictionary中的我的列表框:

主颜色在边框x:Name=original中不起作用

我的标签:

这里的主色很好用

我的ViewModel:

我认为xaml中应该有一些东西不允许我获取MainColor的值

MainColor属性应返回画笔:

还应绑定到父列表框的DataContext:

<Page.Resources>
    <ResourceDictionary Source="/Resources/ResourcePageCuttingData.xaml"></ResourceDictionary>
</Page.Resources>
<ListBox ItemsSource="{Binding Material}" SelectedItem="{Binding SelectedMat, Mode=TwoWay}" Style="{StaticResource styleMat}"   Grid.Column="5" Grid.Row="1" Margin="20,0"></ListBox>
<Label Style="{StaticResource Label_Search}"  Content="{DynamicResource so_lbl_cm}" ></Label>
<Style x:Key="styleMat" TargetType="{x:Type ListBox}">
    <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
    <Setter Property="ItemContainerStyle" Value="{DynamicResource ResourceKey=styleLocMschItem}"></Setter>
    <Setter Property="ItemTemplate" Value="{StaticResource ResourceKey=ResultMatDataTemplate}"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border BorderBrush="#5A5A5A" BorderThickness="1" CornerRadius="4">
                    <ScrollViewer Margin="1">
                        <ItemsPresenter Margin="1"></ItemsPresenter>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<DataTemplate x:Key="ResultMatDataTemplate">
    <TextBlock Text="{Binding}"
                   FontSize="20"
               ></TextBlock>
</DataTemplate>

<Style  x:Key="styleLocMachItem" TargetType="{x:Type ListBoxItem}">
    <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid x:Name="grid">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Border x:Name="hover"
                        Background="YellowGreen"
                        Visibility="Collapsed">
                    </Border>
                    <Border x:Name="orginal"
                        Background="{Binding MainColor}">
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="highlight"
                            Property="Visibility"
                            Value="Visible">
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="Label_Search" TargetType="{x:Type Control}">
    <Setter Property="Background" Value="{Binding MainColor}" />
</Style>
public CuttingSpeed_ViewModel()
    {
        switch (value)
        {
            case "Fast":
                MainColor = "Pink";
                    break;        
             case "Slow":
                MainColor = "Yellow";
                    break;
            default:
                   MainColor ="Red";
                 break;
        }}
    private string _MainColor;

    public string MainColor
    {
        get { return _MainColor; }
        set { _MainColor = value; OnPropertyChanged("MainColor"); }
    }
    public IEnumerable<string> Material
    {
        get { return _Material; }
        set { _Material = value; OnPropertyChanged("Material"); }
    }
    private string _SelectedMat;

    public string SelectedMat
    {
        get { return _SelectedMat; }

        set
        {..}}
public CuttingSpeed_ViewModel()
{
    switch (value)
    {
        case "Fast":
            MainColor = Brushes.Pink;
            break;
        case "Slow":
            MainColor = Brushes.Yellow;
            break;
        default:
            MainColor = Brushes.Red;
            break;
    }
}

private Brush _MainColor;
public Brush MainColor
{
    get { return _MainColor; }
    set { _MainColor = value; OnPropertyChanged("MainColor"); }
}
<Border x:Name="orginal" Background="{Binding DataContext.MainColor, 
        RelativeSource={RelativeSource AncestorType=ListBox}}">