2016-05-02 10 views
0

ラベルとネストされたTextBlock(テキストラッピング用)を使用してWPF UserControlを作成しました。 ドラッグアンドドロップ後にUserControlをコピーする必要があるため、UserControl-XAMLで名前を使用することはできません。 XAMLからすべての名前を削除し、ViewModelを使用する必要がありました。 しかし、ネストしたTextBlockのDependencyPropertiesには問題があります。 例ごとにFonzSizeプロパティを設定しようとすると、反応しません。 「ControlFontSize」のような新しい名前のプロパティを定義すると、それが機能することがわかりました。 しかし、可能ならば、propertyname "FontSize"を使用したいと思っています。 これを処理する方法はありますか?ViewModelでTextBlockのFontSizeプロパティを設定する方法

これはユーザーコントロール-XAMLです:

<UserControl x:Class="WpfDesignerControlLibrary.ControlLabel" 
      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" 
      xmlns:local="clr-namespace:WpfDesignerControlLibrary" 
      mc:Ignorable="d" 
      d:DesignHeight="30" 
      d:DesignWidth="150"> 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Resources.xaml"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </UserControl.Resources> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Border Style="{StaticResource usercontrolBorderStyle}" 
       BorderBrush="{Binding Path=BorderBrush, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
       BorderThickness="{Binding Path=BorderThickness, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"> 
      <Label Margin="2" 
        HorizontalContentAlignment="{Binding Path=ControlHorizontalContentAlignment, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
        VerticalContentAlignment="{Binding Path=ControlVerticalContentAlignment, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"> 
       <TextBlock Text="{Binding Path=Text, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
          FontSize="{Binding Path=ControlFontSize, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
          TextAlignment="{Binding Path=ControlTextAlignment, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
          FontWeight="{Binding Path=FontWeight, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
          FontStyle="{Binding Path=FontStyle, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
          TextDecorations="{Binding Path=TextDecorations, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
          TextWrapping="Wrap"/> 
      </Label> 
     </Border> 
    </Grid> 
</UserControl> 

ViewModelに、プロパティを取得および設定し、ユーザーコントロールのCS-ファイルでのプロパティのこと:

public new double FontSize 
{ 
    get { return viewmodel.FontSize; } 
    set { viewmodel.FontSize = value; } 
} 

そして、これはありますViewModel-プロパティ自体:

public double FontSize 
{ 
    get { return fontSize; } 
    set 
    { 
     fontSize = value; 
     RaisePropertyChanged("FontSize"); 
    } 
} 

答えて

0

私たちは自分自身を見つけました。 TextBlockのFontSizeプロパティをその祖先(この場合はUserControl自体)にバインドできます。ユーザーコントロールのXAMLで

 <TextBlock Text="{Binding Path=Text, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
        FontSize="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=FontSize}" 

、あなたはその後、いつものように、ユーザーコントロールののFontSize-プロパティを使用することができtestwindowインチ 新しいNameまたはDependencyPropertyを持つプロパティは必要ありません。

関連する問題