1
我有以下圖表設計器,我試圖實現更改圖中文本字體大小的功能。右上角的組合框負責這樣做。以編程方式更新DependencyProperty
<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??
}
組合框由ApplicationToolbar.xaml以下XAML代碼呈現通過以下文本框:
<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方法內更新它,因爲它是靜態的。
關於什麼是錯我的做法是appricated :)
試投默認值'short'任何提示: '新的FrameworkPropertyMetadata((簡稱)11)'。目前它被解釋爲'int'。順便說一句,爲什麼'短'?它是簽名類型。我可以理解字節,ushort或int默認情況下:\ – ASh
也我認爲'TextFontSizeProperty'而不是'FontSizeProperty'應該在setter中使用getter:get {return(short)GetValue(TextFontSizeProperty); }','set {SetValue(TextFontSizeProperty,value); }' – ASh
@ASh我使用ushort類型和typecast,它有助於解決前兩個問題,謝謝:)現在我需要了解如何更改組合框選擇更改時如何更改該屬性。 – eXPerience