0

と で を作成しUserControlましProgressBarLabel

<Label Content="{Binding ElementName=UserControl, Path=StatusProperty}" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="76,0,0,32" Name="lblStatus" VerticalAlignment="Bottom" Grid.RowSpan="2" />
<ProgressBar Grid.Row="2" Height="20" HorizontalAlignment="Left" Margin="12,3,0,0" Name="pbCheckProgress" Style="{DynamicResource ProgressBarStyle}" Maximum="{Binding ElementName=UserControl, Path=MaximumProperty}" Minimum="{Binding ElementName=UserControl, Path=MinimumProperty}" Value="{Binding ElementName=UserControl, Path=ValueProperty}" VerticalAlignment="Top" Width="156" />

次に、次のものを作成しましたDependencyProperties

// Dependency Properties for labels
public static readonly DependencyProperty StatusProperty = DependencyProperty.Register("Status", typeof(string), typeof(UserControl1), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

// Dependency Properties for progress bar properties
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(UserControl1), new FrameworkPropertyMetadata(0.0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(UserControl1), new FrameworkPropertyMetadata(0.0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(UserControl1), new FrameworkPropertyMetadata(0.0d, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

ここで、同じページにこれの複数のインスタンスを作成し、 MVVM を使用してコード ビハインドからUserControl更新したいと考えています。ProgressBarしかし、私はこれを理解できませんでした。UserControlすべてのインスタンスが独自の ViewModel のコピーを持つように、ViewModel が必要ですか。

ページ ViewModel からすべてのインスタンスを更新する方法はありますか?

よろしくお願いします、バーラト

4

1 に答える 1

1

私があなたのことを正しく理解していれば、このためにViewModelを作成し、そのViewModelの複数のインスタンス( ViewUserControlのインスタンスごとに1つ)を作成したいようです。UserControl複数がバインドされた単一の ViewModel がある場合UserControls、それらはすべてまったく同じものを表示します。

ページの ViewModel が既にあるように思われるので、次のUserControlように、ページの ViewModel のプロパティとしてViewModel を追加するだけです。

public class PageViewModel : INotifyPropertyChanged
{
    private UCViewModel _ucViewModel;

    //Other ViewModel Code

    public UCViewModel UserControlViewModel
    {
        get { return _ucViewModel; }
    }
}

UCViewModel は、 の ViewModel ですUserControl。次に、XAML で、次のように UCViewModel のプロパティにバインドするだけです。

<local:myControl Status="{Binding UserControlViewModel.Status}"... />

UCViewModel のコレクションがある場合は、少し異なる方法で処理する必要がありますが、概念は同じです。

于 2013-03-06T19:59:06.607 に答える