1

UserControl (DiagramControl という名前) に取り組んでいますが、問題があります。UserControl のコードは次のとおりです。

<UserControl>
   <Border>
      <Grid>
         <ScrollViewer x:Name="DesignerScrollViewer" ... />               
         ...
         <s:ZoomBox x:Name="zoomBox"
                   ScrollViewer="{Binding ElementName=DesignerScrollViewer}"/>
      </Grid>
   </Border>
</UserControl>

ZoomBox (スクロールビューアー DP を持つ) を DesignerScrollViewer にバインドしています。ZoomBox と DesignerScrollViewer が同じ XAML ファイルにあるため、使用する方法は正常に機能するようになりました。

ただし、私がやりたいことは、コントロールから ZoomBox を削除し、コントロールを使用するウィンドウで定義することです。たとえば、次のようなものです。

<Grid>
   <s:DiagramControl x:Name="DC" ... />
   ...
   <s:ZoomBox ScrollViewer={Binding ElementName=DC,Path=DesignerScrollViewer}/>
</Grid>

これを試しましたが、うまくいきません。必要なバインディングを実行するにはどうすればよいですか?

アップデート:

私は自分が何をする必要があるかを理解しました。ScrollViewer DP を DiagramControl に追加し、DesignerScrollViewer のレイアウトが更新されたときに更新するようにしました。したがって、以下は私のDiagramControlコードビハインドに入りました:

        public static readonly DependencyProperty ScrollViewerProperty =
            DependencyProperty.Register("ScrollViewer", typeof(ScrollViewer), typeof(DiagramControl), new UIPropertyMetadata(null, ScrollViewer_PropertyChanged));

        public ScrollViewer ScrollViewer
        {
            get { return (ScrollViewer)GetValue(ScrollViewerProperty); }
            set { SetValue(ScrollViewerProperty, value); }
        }

        private static void ScrollViewer_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {                
        }

        private void DesignerScrollViewer_LayoutUpdated(object sender, EventArgs e)
    {
        ScrollViewer = DesignerScrollViewer;
    }

ScrollViewer DP に更新ソースがあるので、DiagramControl を使用してウィンドウからそれにバインドできました。これが将来誰かに役立つことを願っています。

4

1 に答える 1

1

まず、UI コントロールをビューモデルのプロパティに配置しないでください (正当な理由があります - ビューモデルにすべての情報を保持させ、代わりにそれにバインドします) - あなたの構造 (ビュー <-> ビューモデル) も取得します「関与」し、ますます多くのハッキングにつながります-それは邪魔になりません:) ...

私がこれを正しく理解しているなら、あなたは何を求めている...

あなたDesignerScrollViewerは単なる「子コントロール」であり、バインディングマークアップ拡張はそれを認識していません。

そのようなものが必要な場合は、 DesignerScrollViewerProperty上に作成し、UserControlそれをパスに入れます(ElementNameパーツを保持します)。それは単なるプロパティにすることができます(INotifyPropertyChangedイベントを発生させる必要があります)。

または少し違う、何かのような...

あなたのウィンドウのどこかに:

<my:ZoomBox DataContext="{Binding ElementName=diagramControl, Path=.}"></my:ZoomBox>
<my:DiagramControl x:Name="diagramControl"></my:DiagramControl>

あなたのZoomBox:

<UserControl x:Class="YourApp.ZoomBox" ...="">
    <StackPanel>
        <Button x:Name="GoUp" Command="{Binding UpCommand}" Content="Go Up" IsDefault="False" />
        <Button x:Name="GoDown" Command="{Binding DownCommand}" Content="Go Down" IsDefault="False" />
    </StackPanel>
</UserControl>

あなたのDiagramControl:

<UserControl x:Class="YourApp.DiagramControl" ...="">
    <Grid>
        <ScrollViewer x:Name="_scrollViewer" >
            ...
        </ScrollViewer>
    </Grid>
</UserControl>

...そしてダイアグラム コード ビハインド...

public partial class DiagramControl : UserControl, INotifyPropertyChanged
{
    RelayCommand _upCommand;
    RelayCommand _downCommand;
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName) { this.PropertyChanged.Raise(this, new PropertyChangedEventArgs(propertyName)); }

    public DiagramControl()
    {
        InitializeComponent();
    }

    public RelayCommand UpCommand 
    { 
        get 
        {
            return _upCommand ?? (_upCommand = new RelayCommand(
                param =>
                {
                    _scrollViewer.LineUp();
                },
                param => true)); 
        } 
    }

    public RelayCommand DownCommand
    {
        get
        {
            return _downCommand ?? (_downCommand = new RelayCommand(
                param =>
                {
                    _scrollViewer.LineDown();
                },
                param => true));
        }
    }
}

...(の)代わりに DP にバインドできますDataContext。そして、アップ/ダウンの代わりに、必要なアクション (ズームイン/アウトなど) を配置します。

これはまだ適切ではありません.ダイアグラムとズームボックスを同じビューモデルにバインドする必要があります.コマンドを処理したりTwoWay、値を前後に循環 させるためのバインディングを作成したりするために、いくつかのイベントが必要になるため、少し。

于 2013-03-22T01:01:34.807 に答える