P
P
postya2019-12-22 13:28:29
WPF
postya, 2019-12-22 13:28:29

How to create combobox with custom fonts in code?

Application in C# + WPF
There is a combobox
There are custom fonts that are installed in the system. For example "Journal.ttf", "Alex Brush.ttf" It is
necessary to make it so that not all system fonts are displayed in the combobox, but only those that I specify somewhere.
It is also necessary that these fonts are rendered with their own style. That is, to look in the combobox as they look when applied to text, for example.
Something like this, but so that not all system fonts are displayed, but only those selected by me: I
5dff48bedc280121085480.jpeg
need a programmatic way, since in the future I will save the values ​​\u200b\u200bof the selected fonts from copmbobox to the settings file and apply to different labels.
How to do it?

<ComboBox Style="{StaticResource Combobox}"
                  Name="Fonts"
                  Grid.Row="0" />

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Rumyantsev, 2019-12-22
@postya

ComboBox elements can be not only strings, but almost anything. For example Label.

<ComboBox>
    <ComboBoxItem>
        <Label FontFamily="Font1">Font1</Label>
    </ComboBoxItem>
    <ComboBoxItem>
        <Label FontFamily="Font2">Font2</Label>
    </ComboBoxItem>
</ComboBox>

S
Sumor, 2019-12-22
@Sumor

Take the FontFamily list and assign it to the Source ComboBox.

<ComboBox>
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <TextBlock FontFamily="{Binding}" Text="{Binding Name}" />
    <DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

E
Evgeny Zaletsky, 2019-12-22
@JZ_52

Write them down.

combox.Font = new Font(label1.font.name, w, label1.font.style)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question