2017-10-03 4 views
0

MVVM 프레임 워크를 사용하지 않고 MVVM 응용 프로그램을 빌드하려고합니다. 메인 윈도우와 뷰 모델, appcontroller, 두 개의 뷰 (UserControl이 일반적으로 추천하는 것처럼)를 정의했습니다. 동일한 모양을 공유하는 메인 윈도우에 표시됩니다. & 일반적인 스타일을 포함하는 다른 usercontrol과 컴포지션을 사용하여 느낌이 들었습니다. 아주 좋아.usercontrol 간의 종속성 공유

내 문제는 다음과 같습니다. 모든 내보기 "개체"가 몇 가지 종속성 속성 (보기 제목, 도움말 컨텍스트 등)을 공유하고 싶습니다.

문제는 : 당신이 여러 개체에 같은 DP 이름을 넣을 수 없습니다, 당신은 디자인 모드에서 UserControl에서 상속 뭔가를 수정하지 수

(디자이너는 창, UserControl을하고 페이지 객체를 인식하는 것 같다 VS) 나는 무언가를 여기에서 놓친다 고 생각하지만 손가락을 그 위에 놓을 수는 없다. 나 좀 도와 줄 수있어?

답변

0

마지막으로 처리했습니다.

  • UserControl에서 상속 제어 (전용 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> 
    
관련 문제