2012-05-09 29 views
0

我浏览了本网站上提供的所有答案,以解答我的问题,并找不到工作解决方案。在选择项目后更改组合框背景和前景

我有一个组合框使用ItemsSource属性绑定到一个类。

类定义如下:

public class DataSource 
{ 
    public string DisplayField { get; set; } 
    public string ValueField { get; set; } 
} 

组合框被绑定到显示使用的DisplayMemberPath =“DisplayField”和SelectedValuePath =“ValueField”数据...这在代码进行后面并在窗口加载时加载。

组合框的定义如下:

<ComboBox Grid.Row="0" Grid.Column="1" Margin="5 5 5 0" Name="releaseHoldDropDown" Width="100"/> 

我需要一种方法,如果后台以绿色和前景更改为白色的下降值下降变为“释放”。

如果下拉列表的值更改为“保持”,我还需要将背景更改为红色,将前景更改为白色。

仅供参考:我正在使用ExpressionLight.xaml主题对整个应用程序进行样式设置。

作为一个便笺,我也想要一种方法来改变所有我的组合框从灰色到白色的背景,使它们更具可读性。所以我需要修改ExpressionLight.xaml,但我不知道要编辑哪个部分来进行这些更改。

任何帮助将被赞赏。

谢谢

回答

0

为什么不使用style.trigger?

 <ComboBox.Style> 
      <Style TargetType="ComboBox"> 
       <Setter Property="Background" Value="Transparent"/> 
       <Setter Property="Foreground" Value="Black"/> 
       <Style.Triggers> 
        <Trigger Property="SelectedValue" Value="Release"> 
         <Setter Property="Background" Value="Green"/> 
         <Setter Property="Foreground" Value="White"/> 
        </Trigger> 
        <Trigger Property="SelectedValue" Value="Hold"> 
         <Setter Property="Background" Value="Red"/> 
         <Setter Property="Foreground" Value="White"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ComboBox.Style> 

这可能是一个开端,你

+0

开始我会试试看。 – SamN

+0

我试过你的解决方案,但它没有奏效。 ComboBox的背景没有改变。它周围出现一个白色边框,但组合框背景本身没有改变。 – SamN

+0

我试着用白色背景值解决问题,并且工作正常。 我想知道我需要做什么才能使其成为表达灯主题中的通用样式? – SamN

0

既然你只想要更改前景色,而不是背景色(这仍然是白色的),所以在releaseHoldDropDown_SelectionChanged事件中使用

private void releaseHoldDropDown_SelectionChanged(object sender, FooBar e) 
{ 
    releaseHoldDropDown.ForeGround = new SolidColorBrush(Colors.White); 

    DataSource ds = (DataSource)releaseHoldDropDown.SelectedItem; 

    if (ds.DisplayField == "Release") 
     releaseHoldDropDown.Background = new SolidColorBrush(Colors.Green); 
    else if(ds.DisplayField == "Hold") 
     releaseHoldDropDown.Background = new SolidColorBrush(Colors.Red); 
} 

和好友,我可以帮助你,如果你能给我ExpressionLight.xaml。那么只有我可以帮助

+0

我想在xaml中做到这一点。我需要控制背景为白色或透明,从 – SamN

0

你试图改变模板对照的必要颜色象下面这样?

<ComboBox x:Name="comboBox" 
      ItemsSource="{Binding Items}" 
      Margin="0,0,0,10" 
      Background="White"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <Grid Background="White"> 
       <TextBlock Foreground="Black" Text="{Binding Name}"/> 
      </Grid>  
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox>