【解決方法】datagridcombobox WPF の項目で異なるフォントの太さを使用する必要があります


皆さんこんにちは。
フィールドにアイテムがバインドされたデータグリッドがあります。

特定のアイテムを太字で表示する方法がわかりません。
「Normal」または「Bold」を返す Converter={StaticResourceombogroupitem} を使用します

何か案が?

XML
  <DataGridComboBoxColumn Width="200"
  Header="Description"
                                           
  ItemsSource="{Binding Source={StaticResource inventory_desc_priceViewSource}}"
  SelectedValueBinding="{Binding FK_Inventory, Mode=TwoWay, 
  UpdateSourceTrigger=PropertyChanged}"
  SelectedValuePath="PK_Inventory"
  DisplayMemberPath="Description">

  <DataGridComboBoxColumn.EditingElementStyle>
  <Style TargetType="{x:Type ComboBox}">
  <EventSetter Event="Selector.SelectionChanged" Handler="SomeSelectionChanged" />

                                       
<Setter Property= "ItemsControl. FontWeight"  Value="{Binding Is_group,Source={StaticResource inventory_desc_priceViewSource}, Converter={StaticResource combogroupitem}}"/>
                        
  </Style>

</DataGridComboBoxColumn.EditingElementStyle>

</DataGridComboBoxColumn>

私が試したこと:

WPF データグリッドにはその実装がないため、EditingControlShowing を使用できません。

解決策 30

[Just reaslised this was an old question … I’ll leave the answer/solution for anyone that finds this and requires a solution]

あなたの質問を正しく理解できたら、 FontWeightComboBoxItem 編集モードのドロップダウンリストで 大胆な 項目が「グループ」の場合。

これを行うには、 ComboBoxItem. 列自体で直接これを行うことはできませんが、 Resource のために DataGrid または、必要な特異性に応じて、より高いレベルで。 Blow は、これを行う方法の実例です。

1. データを含む CodeBehind:

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public List<string> Choices {get; set;} = new()
    {
        "Choice 1",
        "Choice 2",
        "Choice 3"
    };

    public ObservableCollection<Person> Items { get; set; } = new()
    {
        new() { Name = "Freddie", Age = 21, Choice="Choice 1" },
        new() { Name = "Milly", Age = 18, Choice="Choice 2" },
        new() { Name = "Caddie", Age = 23, Choice="Choice 3" },
    };

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Items.RemoveAt(0);
    }
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Choice { get; set; }
}

2. ビュー:

XML
<Window x:Class="WpfDataGridComboBoxStyling.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfDataGridComboBoxStyling"
        mc:Ignorable="d" x:Name="Window1"
        Title="MainWindow" Height="450" Width="800">

    <Grid DataContext="{Binding ElementName=Window1}">
        <Grid.Resources>
            <CollectionViewSource x:Key="ChoicesCVS"
                                  Source="{Binding Choices}" />
        </Grid.Resources>

        <DataGrid AutoGenerateColumns="False"
                  ItemsSource="{Binding Items}">
            <DataGrid.Resources>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Margin" Value="8 2" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                <TextBlock Text="{Binding}">
                                    <TextBlock.Style>
                                        <Style TargetType="TextBlock">
                                            <Style.Triggers>
                                                <Trigger Property="Text" Value="Choice 2">
                                                    <Setter Property="FontWeight" Value="Bold" />
                                                    <Setter Property="Foreground" Value="Blue" />
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <DataGridTextColumn Header="Age" Binding="{Binding Age}" />
                <DataGridComboBoxColumn Header="Mode" Width="SizeToHeader" 
                                        SelectedItemBinding="{Binding Choice}"
                                        ItemsSource="{Binding Source={StaticResource ChoicesCVS}}">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="Foreground" Value="Red" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
            
        </DataGrid>
    </Grid>

</Window>

ノート: の完全なテンプレートを実装していません ComboBoxItem 解決策に集中できるようにします。 完全なテンプレートは次の場所にあります。 ComboBox のスタイルとテンプレート – Microsoft Docs[^]

お役に立てれば!

コメント

タイトルとURLをコピーしました