1
votes

I use caliburn.micro in WPF. Although i set x:Name to my element(which is in xaml), i do not see elements (button, textbox, grid etc) in my code behind(In my ViewModel). As if x:Name is private

For Example:

<Button x:Name="mybutton">    
        <StckPanel Orientation="Horizontal">
                <Image Width="30" Source="/Resources/edit.png" />
                <TextBlock Margin="5" FontSize="15" Text="Update"> 
                </TextBlock>
        </StackPanel>
</Button>
2
x:Name creates a private member by default, and you shouldn't change that. Your view model should by definition not access view elements.Clemens

2 Answers

1
votes

X:Name doesn't expose your View Elements to ViewModel, in fact, as Clemens rightly pointed out, you shouldn't try to access the View Elements from View Model.

If you are intending to Invoke a method on Button Click (from above xaml) using the Caliburn Micro conventions, you should write a method that corresponds to the x:Name. For example,

public void mybutton()
{
  // Do something
}

This would invoke mybutton() method each time your button with x:Name mybutton is clicked.

1
votes

You cant access your XAML elements with name in ViewModel. You can only access your XAML elements in code behind file only. For example, your XAML is MainWindow.xaml, then you can access XAML elements in MainWindow.xaml.cs file only. The main purpose of applying MVVM pattern to a project is to make view (xaml) and business logic (ViewModel) loosly coupled. Read more about MVVM here.