【问题标题】:Update DependencyProperty programmatically [duplicate]以编程方式更新 DependencyProperty [重复]
【发布时间】:2017-07-07 00:48:42
【问题描述】:

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

组合框由 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 方法中更新它,因为它是静态的。
任何关于我的方法有什么问题的提示都适用:)

【问题讨论】:

  • 尝试将默认值转换为shortnew FrameworkPropertyMetadata((short)11)。目前它被解释为int。顺便说一句,为什么short?它是签名类型。我可以默认理解 byte、ushort 或 int :\
  • 我也认为TextFontSizeProperty而不是FontSizeProperty应该用在setter的getter中:get { return (short)GetValue(TextFontSizeProperty); },set { SetValue(TextFontSizeProperty, value); }
  • @ASh 我使用了 ushort 类型和 typecast,它帮助解决了前两个问题,谢谢:) 现在我需要了解如何在组合框选择更改时更改该属性..跨度>
  • WPF 中的字体大小为double 值。你也应该使用它。
  • @eXPerience,依赖属性 (DP) 对于不同的 DesignerItem 可以有不同的值。目前我无法在您的代码中找到选择更改后应更新哪些项目

标签: c# wpf xaml


【解决方案1】:

我使用this 帖子中的方法获取我的画布,然后循环更新它的所有项目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多