我有以下图表设计器,其中我试图实现更改图形内文本字体大小的功能。右上方的组合框负责这样做。
组合框由ApplicationToolbar.xaml中的以下XAML代码提供:
<ComboBox
Height="20"
Width="80"
Loaded="{x:Static logic:DesignerItem.RoutedEvent}"
SelectionChanged="{x:Static logic:DesignerItem.SelectionChangedEvent}"
/>
加载/选择的事件是:
public static RoutedEventHandler RoutedEvent = LoadFontSizes;
public static SelectionChangedEventHandler SelectionChangedEvent = FontSizeChanged;
public static void LoadFontSizes(object sender, RoutedEventArgs e)
{
// ... A List.
var sizes = new List<short> { 8,11,14,18,24 };
// ... Get the ComboBox reference.
var comboBox = sender as ComboBox;
// ... Assign the ItemsSource to the List.
comboBox.ItemsSource = sizes;
// ... Make the first item selected.
comboBox.SelectedIndex = 0;
}
private static void FontSizeChanged(object sender, SelectionChangedEventArgs e)
{
// ... Get the ComboBox.
var comboBox = sender as ComboBox;
// ... Set SelectedItem as Window Title.
var s = (short)comboBox.SelectedItem;
// New values comes here, but how to update the dependency property??
}
项目内的文字由以下文本框保存:
<ControlTemplate x:Key="TextBoxDecoratorTemplate" TargetType="{x:Type Control}">
<TextBox Width="Auto" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="{Binding TextFontSize}" Margin="1,1,0,0" AcceptsReturn="True"
Background="Transparent" Text="{Binding Text}"/>
</ControlTemplate>
整个DesignerItem.xaml是:
<ResourceDictionary 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:logic="clr-namespace:DD.Logic;assembly=DD.Logic"
xmlns:itemsConnection="clr-namespace:DD.Logic.ItemsConnection;assembly=DD.Logic"
xmlns:itemDecorators="clr-namespace:DD.Logic.ItemDecorators;assembly=DD.Logic"
mc:Ignorable="d">
<!-- Connector Style -->
<Style TargetType="{x:Type itemsConnection:Connector}">
<Setter Property="Width" Value="8"/>
<Setter Property="Height" Value="8"/>
<Setter Property="Cursor" Value="Cross"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type itemsConnection:Connector}">
<Grid>
<!-- transparent extra space makes connector easier to hit -->
<Rectangle Fill="Transparent" Margin="-2"/>
<Rectangle Fill="Lavender" StrokeThickness="1" Stroke="#AA000080"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- ConnectorDecoratorTemplate Default Template -->
<ControlTemplate x:Key="ConnectorDecoratorTemplate" TargetType="{x:Type Control}">
<Grid Margin="-5">
<itemsConnection:Connector x:Name="Left" Orientation="Left" VerticalAlignment="Center" HorizontalAlignment="Left"/>
<itemsConnection:Connector x:Name="Top" Orientation="Top" VerticalAlignment="Top" HorizontalAlignment="Center"/>
<itemsConnection:Connector x:Name="Right" Orientation="Right" VerticalAlignment="Center" HorizontalAlignment="Right"/>
<itemsConnection:Connector x:Name="Bottom" Orientation="Bottom" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
<!-- ResizeDecorator Default Template -->
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
<Grid Opacity="0.7" SnapsToDevicePixels="true">
<itemDecorators:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<itemDecorators:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
<itemDecorators:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<itemDecorators:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
<itemDecorators:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
VerticalAlignment="Top" HorizontalAlignment="Left"/>
<itemDecorators:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
VerticalAlignment="Top" HorizontalAlignment="Right"/>
<itemDecorators:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<itemDecorators:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</Grid>
</ControlTemplate>
<!-- DragThumb Default Template -->
<Style TargetType="{x:Type itemDecorators:DragThumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type itemDecorators:DragThumb}"/>
</Setter.Value>
</Setter>
</Style>
<!-- TextBoxDecorator Default Template -->
<ControlTemplate x:Key="TextBoxDecoratorTemplate" TargetType="{x:Type Control}">
<TextBox Width="Auto" Height="Auto" VerticalAlignment="Center" HorizontalAlignment="Center"
FontSize="{Binding TextFontSize}" Margin="1,1,0,0" AcceptsReturn="True"
Background="Transparent" Text="{Binding Text}"/>
</ControlTemplate>
<!-- DesignerItem Style -->
<Style TargetType="{x:Type logic:DesignerItem}">
<Setter Property="MinWidth" Value="25"/>
<Setter Property="MinHeight" Value="25"/>
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type logic:DesignerItem}">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
ContextMenu="{StaticResource DesignerItemContextMenu}">
<!-- DragThumb -->
<itemDecorators:DragThumb x:Name="DragThumb" Cursor="SizeAll"/>
<!-- ResizeDecorator -->
<Control x:Name="ResizeDecorator" Visibility="Collapsed"
Template="{StaticResource ResizeDecoratorTemplate}"/>
<!-- ContentPresenter -->
<ContentPresenter x:Name="ContentPresenter" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Content="{TemplateBinding ContentControl.Content}"
Margin="{TemplateBinding ContentControl.Padding}"/>
<!-- ConnectorDecorator -->
<Control x:Name="ConnectorDecorator" Visibility="Hidden"
Template="{StaticResource ConnectorDecoratorTemplate}"/>
<!-- TextBoxDecorator -->
<Control x:Name="TextBoxDecorator" Template="{StaticResource TextBoxDecoratorTemplate}"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Text}"/>
<DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}">
<Setter TargetName="ResizeDecorator" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsDragConnectionOver}">
<Setter TargetName="ConnectorDecorator" Property="Visibility" Value="Visible"/>
</DataTrigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="ConnectorDecorator" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我试图将字体大小绑定到以下依赖项属性:
public class DesignerItem
{
public short TextFontSize
{
get { return (short)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
public static DependencyProperty TextFontSizeProperty =
DependencyProperty.Register("TextFontSize", typeof(short),
typeof(DesignerItem),
new FrameworkPropertyMetadata(11));
//rest of the code
}
第一个问题是我无法设置默认值。错误表示默认值类型错误
第二个问题是以编程方式设置TextFontSize属性(例如在项目的构造函数中)会导致相同的错误。
第三个问题是我无法在FontSizeChanged方法中更新它,因为它是静态的。
关于我的方法有什么问题的任何提示都是适当的:)