0

I'm pretty stuck right now, i'm gonna explain my problem and what i want. In my solution i have a mainWindow, in that MainWindow i call the first userControl Who is situated in an userControlLibrary. I'ts a menu with button. I want when i click on the first button of the first userControl, i want put the visibility of the second usercontrol to visible (too situated in the userControlLibrary). But i try many things but no one works.

The first userControl is UC_MenuSlider and UC_Start_Study is the second who have to be visibile after click on the button on the first one. At launch UC_Start_Study is hidden.

This is a part of the code of my Mainwindow:

<Grid Name="MainGrid">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <UserControlLibrary:UC_StartStudy x:Name="UC_Start_Study"  Grid.Column="1" Height="Auto" Width="Auto" Margin="70 0 0 0" Visibility="Hidden"/>

   <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.1*" MaxWidth="240" MinWidth="240" />
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <UserControlLibrary:UC_MenuSlider x:Name="UC_MenuSlider"  Grid.Column="0"/>
   </Grid>

</Grid>

A part of the code of my first UserControl (UC_MenuSlider):

<Grid Name="Grid_Menu" HorizontalAlignment="Stretch">

    <Grid.RowDefinitions>
     <RowDefinition Height="*"/>
    </Grid.RowDefinitions>


    <Button x:Name="Start_Study"   Grid.Row="0" Grid.Column="0"  Margin="0" Content="Start Study" FontSize="16" Click="Start_Study_Click">
    </Button>
</Grid>

At first a basic event,just an event click in my first userControl. with code behind like that:

public void Start_Study_Click(object sender, RoutedEventArgs e)
{
    var startStudy = new UserControlLibrary.UC_StartStudy();
    startStudy.Visibility = Visibility.Visible;
}

Don't works. Then i use 'RoutedEvent' But I don't really understand who it works.

I hope my question was enough clear, thanks in advance for your anwsers

Sheridan
  • 68,826
  • 24
  • 143
  • 183
Unkle-Wik
  • 3
  • 1
  • 4
  • 1
    Most reliable would be to use *binding*. Provide a dependency property in `UserControl1`, which value should control `UserControl2` visibility, bind `UserControl2` visibility to it (if you use `bool`, then you have to use converter, see [this](http://stackoverflow.com/q/7000819/1997232) question). – Sinatr Oct 10 '14 at 09:02
  • ok i'm going see this thanks – Unkle-Wik Oct 10 '14 at 09:06

2 Answers2

0

The problem is because you are creating a new UC_StartStrudy and set its Visibility to Visible. What you really need is to set Visibility of the one in your XAML: UC_Start_Study

public void Start_Study_Click(object sender, RoutedEventArgs e)
{
   UC_Start_Study.Visibility = Visibility.Visible;
}

And you could also use XAML databinding the Visibility property of your UC_StartStrudy, and set its value in your code:

XAML:

<Window.Resourses>
     <BooleanToVisibilityConverter x:Key="BooltoVisible" />
</Window.Resourse>

.....
<UserControlLibrary:UC_StartStudy x:Name="UC_Start_Study"  Grid.Column="1" Height="Auto" Width="Auto" Margin="70 0 0 0" Visibility="{Binding IsUCStartStudyVisible,  Converter={StaticResource BooltoVisible}}"/>

Code (remember to implement INotifyPropertyChanged ):

//implement INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;


private void RaisePropertyChange(String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

//property for data binding
private bool _isucstartstudyvisible = false;
public bool IsUCStartStudyVisible
{
   get{return _isucstartstudyvisible;}
   set{_isucstartstudyvisible=value; RaisePropertyChange("IsUCStartStudyVisible");}
}

//your event to change the visibility
public void Start_Study_Click(object sender, RoutedEventArgs e)
{
   IsUCStartStudyVisible=true;
}
Bolu
  • 8,696
  • 4
  • 38
  • 70
  • @Unkle-Wik, you need to implement [`INotifyPropertyChanged`](http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx) see my updated answer. – Bolu Oct 10 '14 at 13:22
0

I don't understand why you are taking a new instance of UC_StartStudy() as you have already added this in your MainWindow.

Can't you simply turn the visibility of UC_Start_Study as visible within the code.

Let me show you how you can do this. try

  public void Start_Study_Click(object sender, RoutedEventArgs e)
        {
              this.UC_Start_Study.Visibility = Visibility.Visible;
        }
jadavparesh06
  • 926
  • 7
  • 11