図の中のテキストのフォントサイズを変更する機能を実装しようとしている次の図デザイナーがあります。右上のコンボボックスがそれを担当します。 DependencyPropertyをプログラムで更新する
コンボボックスを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>
<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>
そして私は、次の依存関係プロパティにフォントサイズをバインドしようとしている:0全体DesignerItem.xamlがある
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
}
まず問題は、私はデフォルトを設定することはできませんということです値。エラーは、デフォルト値のタイプが間違っていると言います。
2番目の問題は、TextFontSizeプロパティをプログラムで(たとえばアイテムのコンストラクタで)設定すると、同じエラーが発生することです。
3番目の問題は、静的なのでFontSizeChangedメソッドの内部では更新できないということです。私のアプローチが間違っているかについて
任意のヒントがappricatedされている:)
内の項目がshort' 'にキャストデフォルト値を試すだすべてを更新しました: '新しいFrameworkPropertyMetadata((短い)11)'。現在は 'int'と解釈されます。 btw、なぜ「短い」?それは署名タイプです。私はbyte、ushort、またはintをデフォルトで理解することができます:\ – ASh
また、 'TextFontSizeProperty'ではなく' FontSizeProperty'がsetterのgetterで使われるべきだと思います: 'get {return(short)GetValue(TextFontSizeProperty); } '、' set {SetValue(TextFontSizeProperty、value);} } ' – ASh
@ASh私はushort型と型変換を使用し、最初の2つの問題を解決するのに役立ちました。ありがとうございました:)コンボボックスの選択が変更されたときにそのプロパティを変更する方法を理解する必要があります。 – eXPerience