2017-10-03 10 views
0

MVVMフレームワークを使用せずにMVVMアプリケーションを構築しようとしています。 私は私のメインウィンドウとビューモデル、appcontroller、同じ外観を共有するメインウィンドウのパネルに表示されるビュー(一般的にアドバイスされるように)の2つのビューを定義しました&共通のスタイルを含む別のusercontrolとても良い。ユーザーコントロール間の依存関係プロパティの共有

私の問題は次のとおりです:私のすべてのビュー "オブジェクト"は、いくつかの依存プロパティ(ビュータイトル、ヘルプコンテキストなど)を共有したいと思います。

問題がある:あなたが複数のオブジェクトに同じDPの名前を入れることができない、あなたは は、デザインモードでUserControlから継承何か(デザイナーVSのみ窓、ユーザーコントロールとページオブジェクトを認識すると思われる)

を変更することはできません私はここで何かを見逃しているが、それに指を置くことはできないと思う。手伝って頂けますか ?

答えて

0

最後にやりました。

  • この制御のためのグローバルリソースにスタイルを宣言し、ユーザーコントロール(のみのCSファイル、なしXAML)

    public class BaseView : UserControl 
    { 
    
    
        public string Title 
        { 
         get { return (string)GetValue(TitleProperty); } 
         set { SetValue(TitleProperty, value); } 
        } 
    
        // Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty TitleProperty = 
         DependencyProperty.Register("Title", typeof(string), typeof(BaseView), new PropertyMetadata("New view")); 
    
        public BaseView() 
        { 
    
        } 
    } 
    
  • から継承コントロールを宣言します。

    <Style TargetType="{x:Type views:BaseView}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type views:BaseView}"> 
           <Border CornerRadius="5" BorderThickness="2"> 
            <Grid Name="RootGrid"> 
             <ContentPresenter></ContentPresenter> 
            </Grid> 
           </Border> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
    </Style> 
    
  • することができます後別のコントロールでBaseViewコントロールをサブクラス化します。

    <local:BaseView x:Class="MyNS.NiceView" 
          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" 
          Title="My Software" 
          d:DesignHeight="300" d:DesignWidth="300"> 
        <Grid> 
        <StackPanel> 
         <Button>HAAAAhahahaha</Button> 
         <Label>213456789</Label> 
        </StackPanel>   
    </Grid> 
    </local:BaseView> 
    
関連する問題