我的代码有一些问题,我希望你能帮助我。
我正在尝试学习WPF/XAML,作为学习过程的一部分,我决定制作一个基本的文本编辑器来测试我自己的技能。我想做一个字体大小的组合框,它将根据组合框中的值调整所有文本的大小(基本上就像任何其他文本编辑器一样,但我不知道怎么做。到目前为止,我的XAML如下:
<Window x:Class="DataBinding.UsingCommandsSample"
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:DataBinding"
mc:Ignorable="d"
Title="MainWindow" WindowState="Maximized" >
<DockPanel>
<Menu DockPanel.Dock="Top" Margin="0 0 0 10">
<MenuItem Header="_File" Name="file">
<MenuItem Header="Open File" Click="btnOpenFile_Click"/>
<MenuItem Header="Save File" Click="btnSaveFile_Click">
<MenuItem Header="Save As" Click="btnSaveAs_Click" />
</MenuItem>
<MenuItem Header="Save As" Click="btnSaveAs_Click" />
<MenuItem Command="Print" />
</MenuItem>
<MenuItem Header="_Edit" Name="edit">
<MenuItem Command="Copy" />
<MenuItem Command="Cut" />
<MenuItem Command="Paste" />
<MenuItem Command="SelectAll" />
<MenuItem Command="Undo" />
<MenuItem Command="AlignLeft" />
<MenuItem Command="EditingCommands.AlignCenter" />
<MenuItem Command="AlignRight" />
<MenuItem Command="AlignJustify" />
</MenuItem>
</Menu>
<ToolBarTray DockPanel.Dock="Top" Height="auto">
<ToolBar>
<Button Command="Cut" Content="Cut" />
<Button Command="Copy" Content="Copy" />
<Button Command="Paste" Content="Paste" />
<ComboBox x:Name="fontBox" SelectedValue="selected" SelectionChanged="fontBox_SelectionChanged">
<ComboBoxItem Content="12" IsSelected="True"/>
<ComboBoxItem Content="16" />
<ComboBoxItem Content="18" />
<ComboBoxItem Content="20" />
</ComboBox>
</ToolBar>
</ToolBarTray>
<TextBox Name="txtEditor" AcceptsReturn="True" />
</DockPanel>
</Window>
下面是我的代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using System.IO;
namespace DataBinding
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class UsingCommandsSample : Window
{
public UsingCommandsSample()
{
InitializeComponent();
}
public string path;
private void btnOpenFile_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
txtEditor.Text = File.ReadAllText(openFileDialog.FileName);
path = openFileDialog.FileName;
}
private void btnSaveFile_Click(object sender, RoutedEventArgs e)
{
if (path != null)
File.WriteAllText(path, txtEditor.Text);
else
MessageBox.Show("You have not specified a location to which the file should be saved. Click 'OK' then File >> Save As.", "Cannot find path", MessageBoxButton.OK, MessageBoxImage.Warning);
}
private void btnSaveAs_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Text file (*.txt)|*txt | Banana (*.banana)|*.cs";
if (saveFileDialog.ShowDialog() == true)
File.WriteAllText(saveFileDialog.FileName, txtEditor.Text);
}
private void fontBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int temp;
if (Int32.TryParse(fontBox.Text, out temp))
{
txtEditor.FontSize = temp;
}
}
}
}
由于某些原因,字体大小确实会更新,但只有在我选择不同的ComboBoxItem之后才会更新。例如,如果我从选择"12“开始,然后选择"14",则不会发生任何事情。如果我选择"16",字体大小将变为14。我想象这些操作是以我不希望它们发生的顺序发生的,但我被难倒了,不知道为什么或如何修复。
如果有人能帮我解决这个问题,我将不胜感激。我似乎找不到任何其他资源,所以StackOverflow是我最后的希望。
https://stackoverflow.com/questions/50766015
复制相似问题