尝试从用户控件更改主窗口的DataContext

时间:2018-11-13 13:26:12

标签: c# wpf xaml binding datacontext

在主窗口初始化时,我将DataContext设置为usercontrol,在此usercontrol上,我发生了一个事件,该事件想将主窗口的datacontext更改为另一个usercontrol,但是什么也没有发生。

这是主窗口的xaml:                                                                   

</Window.Resources>
<Grid>
    <ContentControl Content="{Binding}" Width="auto" Height="auto" />
</Grid>

这是主窗口的C#:

public MainWindow()
    {
        InitializeComponent();
        DataContext = new LogInViewModel();
    }

这是LogInUserControl的xaml:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Column="1" Grid.Row="1">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Width="250">
            <StackPanel Width="125">
                <TextBlock Text="Email:" Margin="5,0,5,0" Width="auto"/>
            </StackPanel>
            <StackPanel Width="125">
                <TextBlock Text="Password:" Margin="5,0,0,0" Width="auto"/>
            </StackPanel>
        </StackPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBox Margin="5,0,5,0" HorizontalAlignment="Center" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <PasswordBox Margin="0,0,0,5" HorizontalAlignment="Center" Height="23" VerticalAlignment="Top" Width="120"/>
        </StackPanel>
        <Button Content="Log In" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Top" Width="75"/>
    </StackPanel>
    <Grid Grid.Column="1" Grid.Row="1">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
        <TextBlock Text="don't have account yet ?" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5"/>
        <TextBlock Name="TBSignUp" Text="Sign Up" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" PreviewMouseLeftButtonDown="TextBlock_PreviewMouseLeftButtonDown" PreviewMouseLeftButtonUp="TextBlock_PreviewMouseLeftButtonUp" Foreground="#FF0B36F5"/>
        </StackPanel>
    </Grid>
</Grid>

这是LogInUserControl的C#:

public partial class LogInView : UserControl
{
    string BlackForeground = "#FF000000" ;
    string OriginalForeground = "#FF0B36F5";
    public LogInView()
    {
        InitializeComponent();
    }

    private void TextBlock_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        TBSignUp.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(OriginalForeground));
        DataContext = new RegisterView();
    }

    private void TextBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        TBSignUp.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(BlackForeground));
    }
}

2 个答案:

答案 0 :(得分:1)

在WPF中,您可以从任何地方获取Shell(第一个)窗口:

System.Windows.Window shell = System.Windows.Application.Current.MainWindow;

OR

 Application.Current.Windows[0];

但是我建议根据需要传递参考。

看看MVVM模式,您需要在视图模型类中实现属性更改通知以绑定属性。

答案 1 :(得分:0)

您需要在App.xaml.cs上进行设置。

App.xaml.cs:

public class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
         MainWindow window=new MainWindow();
         LogInViewModel vm=new LogInViewModel(); // You need to set DataContext...
         window.DataContext=vm; // ...before showing up the window.
         window.Show();
    }
 }

在我从研究中发现的ViewModel模式中,用法在DataContext之前,在Show();之后。

我希望能解决您的问题。