如何在SIlverlight中设置所选/当前/活动ComboBoxItem的样式?

时间:2014-08-19 14:36:10

标签: silverlight combobox datagridcomboboxcolumn

由于某种原因,ComboBox中的所选项目根本没有样式。 这是我的用户控制资源

 <UserControl.Resources>
        <Style x:Key="cmbox" TargetType="ComboBox">
            <Setter Property="Background" Value="#3A3D9E"/>
            <Setter Property="Foreground" Value="DarkBlue"/>

        </Style>
        <Style x:Key="cmboxItem" TargetType="ComboBoxItem">
            <Setter Property="Background" Value="#3A3D9E"/>
            <Setter Property="BorderBrush" Value="AliceBlue"/>
            <Setter Property="Foreground" Value="DarkBlue"/>
        </Style>

代码......

 <sdk:DataGridTextColumn Binding="{Binding Benamning}" Header="Benämning"/>
                            <sdk:DataGridTextColumn Binding="{Binding Antal}" Header="Antal"/>
                            <sdk:DataGridTemplateColumn Header="Item">
                                <sdk:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel>
                                            <StackPanel.Resources>
                                                <Style BasedOn="{StaticResource cmbox}" TargetType="ComboBox"/>
                                                <Style BasedOn="{StaticResource cmboxItem}" TargetType="ComboBoxItem"/>
                                            </StackPanel.Resources>
                                            <ComboBox SelectedIndex="0">
                                                <ComboBoxItem >Item1</ComboBoxItem>
                                                <ComboBoxItem >Item2</ComboBoxItem>
                                                <ComboBoxItem >Item3</ComboBoxItem>
                                            </ComboBox>
                                        </StackPanel>
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellTemplate>
                            </sdk:DataGridTemplateColumn>
                                <sdk:DataGridTextColumn Binding="{Binding Dis_saldo}" Header="Disponibelt Saldo"/>
                </sdk:DataGrid.Columns>

PRINTSCREEN:

enter image description here

**编辑:2014-08-20 12:30 GMT。请在此处查看我的更改:**

  <sdk:DataGridTemplateColumn Header="Item">
                                <sdk:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>

                                        <StackPanel>
                                            <StackPanel.Resources>
                                                <Style BasedOn="{StaticResource cmbox}" TargetType="ComboBox"/>
                                                <Style BasedOn="{StaticResource cmboxItem}" TargetType="ComboBoxItem"/>
                                            </StackPanel.Resources>
                                            <ComboBox  x:Name="TheCombo">
                                                <ComboBoxItem >Item1</ComboBoxItem>
                                                <ComboBoxItem >Item2</ComboBoxItem>
                                                <ComboBoxItem >Item3</ComboBoxItem>
                                            </ComboBox>
                                        </StackPanel>
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellTemplate>
                                <sdk:DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <TextBlock  Text="{Binding SelectionBoxItem, ElementName=TheCombo}">
                                        </TextBlock>
                                    </DataTemplate>
                                </sdk:DataGridTemplateColumn.CellEditingTemplate>
                            </sdk:DataGridTemplateColumn>

不起作用:(

1 个答案:

答案 0 :(得分:1)

好的,所以你的问题是你只使用了CellTemplate,你需要加入一个CellEditingTemplate,这样你的ComboBox只会在编辑那个单元格时出现,YourComboBox似乎表现为预期(虽然不确定为什么你会在这样一个黑暗的背景上想要这么深的文字)然后当它失去焦点时你会看到它看起来像你的DataGrid上的其他一切。所以(也见评论)类似的东西;

<sdk:DataGridTextColumn Binding="{Binding Benamning}" Header="Benämning"/>
  <sdk:DataGridTextColumn Binding="{Binding Antal}" Header="Antal"/>
  <sdk:DataGridTemplateColumn Header="Item">
    <sdk:DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
      <!-- PS, ya don't need this StackPanel but I'll leave it in here. -->
        <StackPanel>
          <StackPanel.Resources>
            <Style BasedOn="{StaticResource cmbox}" TargetType="ComboBox"/>
            <Style BasedOn="{StaticResource cmboxItem}" TargetType="ComboBoxItem"/>
          </StackPanel.Resources>
          <ComboBox SelectedIndex="0" x:Name="TheCombo">
            <ComboBoxItem >Item1</ComboBoxItem>
            <ComboBoxItem >Item2</ComboBoxItem>
            <ComboBoxItem >Item3</ComboBoxItem>
          </ComboBox>
        </StackPanel>
      </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
    <sdk:DataGridTemplateColumn.CellEditingTemplate>
      <DataTemplate>
        <!-- Not sure if it's SelectedValue, or SelectedItem, or Text, or whatever
             you'll need to bind to to display the selected item from your ComboBox
             you have in your CellTemplate but you get the jist -->
        <TextBlock Text="{Binding SelectedValue, ElementName=TheCombo}"/>
      </DataTemplate>
    </sdk:DataGridTemplateColumn.CellEditingTemplate>    
  </sdk:DataGridTemplateColumn>
  <sdk:DataGridTextColumn Binding="{Binding Dis_saldo}" Header="Disponibelt Saldo"/>
</sdk:DataGrid.Columns>

希望这会有所帮助,欢呼。