用户控件属性不接受DynamicResource值

时间:2012-06-02 09:45:04

标签: wpf

usercontrol.vb文件

Imports System.Data
Imports Orion_Magnetics_Ascend.StrategicAlliance

Public Class usrValueSet

    Public WriteOnly Property LabelPropertyValueSet() As String
        Set(ByVal value As String)
            lblValueSetsName.Text = value
        End Set
    End Property

End Class

usercontrol.xaml文件

<UserControl x:Class="usrValueSet"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40">
            </RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
         </Grid.ColumnDefinitions>
        <TextBlock Name="lblValueSetsName" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

    </Grid>
</UserControl>

在示例页面上使用如下所示的用户控件

<local:usrValueSet x:Name="TFR_uvsCurrentLimitingFusePanelRating" Master="CurrentLimitingFusePanelRating" LabelPropertyValueSet="{DynamicResource TFR_tbRating}"   MaxTextLength="25"></local:usrValueSet>

如果您可以看到上面的“LabelPropertyValueSet”,我已经为其分配了动态资源

但它给了我错误

  

无法在'usrValueSet'类型的'LabelPropertyValueSet'属性上设置'DynamicResourceExtension'。 'DynamicResourceExtension'只能在DependencyObject的DependencyProperty上设置。

喜欢上面..

请有人知道如何为usercontrol属性分配动态资源......?

1 个答案:

答案 0 :(得分:0)

usercontrol.vb文件需要更改以下需要更改的内容才能生成此属性 DependencyProperty并执行以下更改

Public Shared ReadOnly MyBorderBackgroundProperty As DependencyProperty = DependencyProperty.Register("LabelPropertyValueSet", GetType(String), GetType(usrValueSet), New FrameworkPropertyMetadata(AddressOf OnBackChanged))

        Private Shared Sub OnBackChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
            Dim sender = DirectCast(d, usrValueSet)
            sender.lblValueSetsName.Text = e.NewValue
            'lblValueSetsName.Text = e.NewValue
            ' Put a breakpoint here
        End Sub


    Public WriteOnly Property LabelPropertyValueSet() As String
            Set(ByVal value As String)
                SetValue(MyBorderBackgroundProperty, value)
            End Set
        End Property

之后,您可以将dynamicresource值分配给usercontrol属性 如下所示

  <local:usrValueSet x:Name="TFR_uvsExpulsionFusePanelRating" Master="ExpulsionFusePanelRating"  LabelPropertyValueSet="{DynamicResource ResourceKey=TFR_tbRating}" MaxTextLength="25"></local:usrValueSet>