Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式更新DependencyProperty_C#_Wpf_Xaml - Fatal编程技术网

C# 以编程方式更新DependencyProperty

C# 以编程方式更新DependencyProperty,c#,wpf,xaml,C#,Wpf,Xaml,我在下面的图表设计器中尝试实现更改图形中文本字体大小的功能。右上角的组合框负责执行此操作。 组合框由ApplicationToolbar.XAML中的以下XAML代码显示: <ComboBox Height="20" Width="80" Loaded="{x:Static logic:DesignerItem.RoutedEvent}" SelectionChanged="{x:Static

我在下面的图表设计器中尝试实现更改图形中文本字体大小的功能。右上角的组合框负责执行此操作。

组合框由ApplicationToolbar.XAML中的以下XAML代码显示:

<ComboBox
        Height="20"
        Width="80"
                Loaded="{x:Static logic:DesignerItem.RoutedEvent}"
                SelectionChanged="{x:Static logic:DesignerItem.SelectionChangedEvent}"
        />
第一个问题是我不能设置默认值。错误表明默认值的类型错误。
第二个问题是,以编程方式设置TextFontSize属性(例如在项的构造函数中)会导致相同的错误。
第三个问题是我不能在FontSizeChanged方法中更新它,因为它是静态的。

任何关于我的方法错误的提示都会被告知:)

我使用post中的方法来获取画布,然后更新循环中的所有项

我使用post中的方法来获取画布,然后更新循环中的所有项

尝试将默认值强制转换为
short
新框架属性元数据((short)11)
。目前它被解释为
int
。顺便问一下,为什么
short
?它是有符号类型。默认情况下,我可以理解byte、ushort或int:\而且我认为应该在setter中的getter中使用
TextFontSizeProperty
而不是
FontSizeProperty
get{return(short)GetValue(TextFontSizeProperty);}
set{SetValue(TextFontSizeProperty,value);}
@ASh我使用了ushort-type和typecast,它帮助解决了前两个问题,谢谢:)现在我需要了解如何在组合框选择更改时更改该属性。WPF中的字体大小是
双值。您还应该使用它。@eXPerience,对于不同的DesignerItems,依赖属性(DP)可以有不同的值。目前,我在您的代码中找不到在选择更改后应更新哪些项目,请尝试将默认值转换为
short
new FrameworkPropertyMetadata((short)11)
。目前它被解释为
int
。顺便问一下,为什么
short
?它是有符号类型。默认情况下,我可以理解byte、ushort或int:\而且我认为应该在setter中的getter中使用
TextFontSizeProperty
而不是
FontSizeProperty
get{return(short)GetValue(TextFontSizeProperty);}
set{SetValue(TextFontSizeProperty,value);}
@ASh我使用了ushort-type和typecast,它帮助解决了前两个问题,谢谢:)现在我需要了解如何在组合框选择更改时更改该属性。WPF中的字体大小是
双值。您还应该使用它。@eXPerience,对于不同的DesignerItems,依赖属性(DP)可以有不同的值。目前,我无法在您的代码中找到更改选择后应更新的项目
    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>
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
}