2

WPFアプリケーションでusercontrols内部を表示する別の方法があるかどうか知りたいのですが。mainwindow現在usercontrols、ボタンのクリックで一度に1つのユーザーコントロールを表示するために、の可視性プロパティを利用しています。ユーザーコントロールの可視性をに設定し、Hiddenボタンをクリックすると可視性を変更します。それは完璧に動作します。しかし、これはこれを行う正しい方法ですか?

編集:

私はこのようなことを試みましたが、うまくいきません。

mainwindow.xaml:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication4"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="54*" />
        <RowDefinition Height="257*" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="1">
        <ContentControl Content="{Binding CurrentView}"/>
    </Grid>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="25,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <Button Content="Button" Height="23" HorizontalAlignment="Right" Margin="0,12,251,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>

</Window>

UserControl1とUserControl2の2つのユーザーコントロールがあります。背後にあるコード:

 private UserControl currentview;


    private UserControl CurrentView
    {
        get
        {
            return this.currentview;
        }
        set
        {
            this.currentview = value;
            //RaisePropertyChanged("CurrentView");
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        UserControl1 uc1 = new UserControl1();
        CurrentView = uc1;
    }

それは動作しません。それを行う正しい方法は何ですか?

4

2 に答える 2

4

( MainWindow ContentControlxaml で) を取得し、そのコンテンツをビューにバインドして、コードで切り替えることができます。このような:

<Window x:Class="WpfApplication4.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication4"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="54*" />
        <RowDefinition Height="257*" />
    </Grid.RowDefinitions>

    <ContentControl Grid.Row="0" Content="{Binding CurrentView}"/>

    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="25,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <Button Content="Button" Height="23" HorizontalAlignment="Right" Margin="0,12,251,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>

</Window>

コードビハインド:

    private UserControl currentView;

    public UserControl CurrentView
    {
        get
        {
            return this.currentView;
        }

        set
        {
            if (this.currentView == value)
            {
                return;
            }

            this.currentView = value;
            RaisePropertyChanged("CurrentView");
        }
    }
于 2012-08-21T11:58:22.213 に答える
0

ウィンドウのライフサイクル中にコントロールがメモリに残っていても問題がなければ、何も問題はないと思います。また、アマデウス・ハインの答え。

于 2012-08-21T12:03:49.923 に答える