Я пытаюсь придумать, как повлиять на мой "EditBox"
тремя кнопками:
Я хочу изменить имя и размер шрифта,когда я изменяю переменные comboboxes
.
У меня есть оператор switch и теги, чтобы определить каждую кнопку, которую я пытался изменить размер шрифта, вызывая CharacterFormat
Iуже настроена виртуальная машина для этого, но я сейчас не знаю, как передать тег каждой кнопки
<Page x:Name="Main"
x:Class="uwpEvernote.View.NotesPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:uwpEvernote.View"
xmlns:vm="using:uwpEvernote.ViewModel"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<vm:NotesVM x:Key="VM" />
<SolidColorBrush x:Key="enabled"
Color="#0078D4" />
<SolidColorBrush x:Key="disabled"
Color="Transparent" />
</Page.Resources>
<RelativePanel x:Name="Container"
DataContext="{StaticResource VM}"
Background="{ThemeResource SystemChromeLowColor}"
Loaded="Container_Loaded">
<MenuBar x:Name="menuBar">
<MenuBarItem Title="File">
<MenuFlyoutItem Text="New notebook"
Command="{Binding NewNotebookCommand}">
<MenuFlyoutItem.Icon>
<FontIcon Glyph="" />
</MenuFlyoutItem.Icon>
</MenuFlyoutItem>
<MenuFlyoutItem Text="New Note"
Command="{Binding NewNoteCommand}"
CommandParameter="{Binding SelectedNotebook}">
<MenuFlyoutItem.Icon>
<FontIcon Glyph="" />
</MenuFlyoutItem.Icon>
</MenuFlyoutItem>
<MenuFlyoutSeparator />
<MenuFlyoutItem Text="Exit"
Command="{Binding ExitCommand}">
<MenuFlyoutItem.Icon>
<FontIcon Glyph="" />
</MenuFlyoutItem.Icon>
</MenuFlyoutItem>
</MenuBarItem>
</MenuBar>
<ListView x:Name="Notebook"
RelativePanel.Below="menuBar"
Width="140"
SelectedItem="{Binding SelectedNotebook, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding NoteBooks}"
RelativePanel.AlignBottomWithPanel="True">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView x:Name="Notes"
Width="140"
ItemsSource="{Binding Notes}"
RelativePanel.Below="menuBar"
RelativePanel.RightOf="Notebook"
RelativePanel.AlignBottomWithPanel="True">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<CommandBar x:Name="commandBar"
RelativePanel.Below="menuBar"
RelativePanel.RightOf="Notes"
RelativePanel.AlignRightWithPanel="True"
OverflowButtonVisibility="Collapsed"
VerticalAlignment="Center"
Margin="0,10,20,10"
Background="{ThemeResource SystemChromeLowColor}">
<CommandBar.Content>
<StackPanel Orientation="Horizontal">
<AppBarButton x:Name="textToSpeech"
Icon="Microphone"
Click="Actions_Click"
Tag="0"
ToolTipService.ToolTip="Text to speech" />
<AppBarButton x:Name="FormatBoltText"
ToolTipService.ToolTip="Bold"
Icon="Bold"
Tag="1"
Click="Actions_Click" />
<AppBarButton x:Name="formatItalicText"
ToolTipService.ToolTip="Italic"
Icon="Italic"
Tag="2"
Click="Actions_Click" />
<AppBarButton x:Name="formatUnderlineText"
ToolTipService.ToolTip="Underline"
Icon="Underline"
Tag="3"
Click="Actions_Click" />
<ComboBox IsEditable="True"
Tag="1"
x:Name="fontBox"
SelectionChanged="ComboChanged"
Width="150" />
<ComboBox x:Name="fontSizeBox"
Tag="2"
SelectionChanged="ComboChanged"
IsEditable="True"
Margin="10,0,0,0"
/>
</StackPanel>
</CommandBar.Content>
</CommandBar>
<RichEditBox x:Name="richEbitBox"
TextChanged="Cotent_TextChanged"
RelativePanel.RightOf="Notes"
RelativePanel.Below="commandBar"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignBottomWith="Notebook"
Margin="0,0,10,40" />
<CommandBar Background="{ThemeResource SystemChromeLowColor}"
RelativePanel.RightOf="Notes"
RelativePanel.AlignBottomWith="richEbitBox"
RelativePanel.AlignRightWithPanel="True"
Margin="0,0,10,0"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<CommandBar.Content>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,10,0,0">
<TextBlock Text="Count"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
<TextBlock Text="|"
Margin="10,0,0,0" />
<TextBlock x:Name="charactersCount"
Margin="10,0,0,0" />
</StackPanel>
</CommandBar.Content>
</CommandBar>
</RelativePanel>
public ObservableCollection<NoteBook> NoteBooks { get; set; }
private NoteBook _SelectedNotebook;
public NoteBook SelectedNotebook {
get { return _SelectedNotebook; }
set {
if (value != _SelectedNotebook) {
_SelectedNotebook = value;
//OnPropertyChanged("SelectedNotebook");
}
}
}
public ObservableCollection<Note> Notes { get; set; }
public NewNoteCommand NewNoteCommand { get; set; }
public NewNotebookCommand NewNotebookCommand { get; set; }
public ExitCommand ExitCommand { get; set; }
public NotesVM() {
NewNoteCommand = new NewNoteCommand(this);
NewNotebookCommand = new NewNotebookCommand(this);
ExitCommand = new ExitCommand(this);
NoteBooks = new ObservableCollection<NoteBook>();
Notes = new ObservableCollection<Note>();
ReadNotebooks();
}
public void CreateNotebook() {
var newNotebook = new NoteBook() {
Name = "New notebook"
};
DatabaseHelper.Insert(newNotebook);
}
public void CreateNote(int id) {
var newNote = new Note() {
NotebookId = id,
CratedTime = DateTime.Now,
UpdatedTime = DateTime.Now,
Title = "New note"
};
DatabaseHelper.Insert(newNote);
}
public void ReadNotebooks() {
using (var conn = new SQLiteConnection(DatabaseHelper.dbFile)) {
var notebooks = conn.Table<NoteBook>().ToList();
NoteBooks.Clear();
foreach (var item in notebooks) {
NoteBooks.Add(item);
}
}
}
public void ReadNoote() {
using (var conn = new SQLiteConnection(DatabaseHelper.dbFile)) {
if (SelectedNotebook != null) {
var notes = conn.Table<Note>().Where(n => n.NotebookId == SelectedNotebook.Id).ToList();
Notes.Clear();
foreach (var item in notes) {
Notes.Add(item);
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
}
public NotesPage() {
this.InitializeComponent();
var fonts = CanvasTextFormat.GetSystemFontFamilies();
fontBox.ItemsSource = fonts;
var arrList = new ArrayList();
for (int i = 0; i < 73; ++i) {
arrList.Add(i);
}
fontSizeBox.ItemsSource = arrList;
}
private void Cotent_TextChanged(object sender, RoutedEventArgs e) {
richEbitBox.Document.GetText(TextGetOptions.None, out string value);
charactersCount.Text = (value.Length - 1).ToString();
}
private async void Actions_Click(object sender, RoutedEventArgs e) {
var id = sender as Button;
switch (id.Tag) {
case "0":
using (SpeechRecognizer recognizer = new SpeechRecognizer()) {
await recognizer.CompileConstraintsAsync();
var result = await recognizer.RecognizeWithUIAsync();
var dialog = new MessageDialog(result.Text, "Text spoken");
await dialog.ShowAsync();
richEbitBox.Document.GetText(TextGetOptions.None, out string value);
richEbitBox.Document.SetText(TextSetOptions.None, value += result.Text);
}
break;
case "1":
if (richEbitBox.Document.Selection.CharacterFormat.Bold == FormatEffect.On) {
richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.Off;
FormatBoltText.Background = (SolidColorBrush)Resources["disabled"];
} else {
richEbitBox.Document.Selection.CharacterFormat.Bold = FormatEffect.On;
FormatBoltText.Background = (SolidColorBrush)Resources["enabled"];
}
break;
case "2":
if (richEbitBox.Document.Selection.CharacterFormat.Italic == FormatEffect.On) {
richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.Off;
formatItalicText.Background = (SolidColorBrush)Resources["disabled"];
} else {
richEbitBox.Document.Selection.CharacterFormat.Italic = FormatEffect.On;
formatItalicText.Background = (SolidColorBrush)Resources["enabled"];
}
break;
case "3":
if (richEbitBox.Document.Selection.CharacterFormat.Underline == UnderlineType.Single) {
richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.None;
formatUnderlineText.Background = (SolidColorBrush)Resources["disabled"];
} else {
richEbitBox.Document.Selection.CharacterFormat.Underline = UnderlineType.Single;
formatUnderlineText.Background = (SolidColorBrush)Resources["enabled"];
}
break;
default:
break;
}
}
private void ComboChanged(object sender, SelectionChangedEventArgs e) {
var id = sender as ComboBox;
switch (id.Tag) {
case "1":
//Todo implement new font
string fontName = id.SelectedItem.ToString();
richEbitBox.Document.Selection.CharacterFormat.Name = fontName;
break;
case "2":
var size = (float)id.SelectedItem;
break;
default:
break;
}
}
private void Container_Loaded(object sender, RoutedEventArgs e) {
fontBox.Text = richEbitBox.Document.GetDefaultCharacterFormat().Name;
fontSizeBox.Text = richEbitBox.Document.GetDefaultCharacterFormat().Size.ToString();
}
}
}
- изменить размер шрифта, когда я выпадаю и выбираю номер, и имя шрифта
- Исключить все из кода позади (я не знаю, если это плохая практикаиспользовать это или нет)