Navigation with MVVM template WPF application [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm planning wpf MVVM application. I read a lot of about MVVM pattern. But I can't find best practices to implement navigation.
I have scenario application starts with login screen and after login i want to have page with navigation menu. How should I handle that?
I read about use Frame, UserControl and viewModel container. But It's not clear to me what should I use.

Well there are 100000 options how to implement navigation
Using DataTemplates
Create DataTemplates in your resources
<DataTemplate DataType="{x:Type local:YOUR_PAGE_VIEWMODEL1}">
<local:YOUR_PAGE1/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:YOUR_PAGE_VIEWMODEL2}">
<local:YOUR_PAGE2/>
</DataTemplate>
NOTE: YOUR_PAGE_VIEWMODEL1 and YOUR_PAGE_VIEWMODEL2 have the same base class in our case (lets call it BasePageViewModel)
then in your MainViewModel you can add something like this
class MainViewModel : INotifyPropertyChanged
{
//....
public BasePageViewModel CurrentPage { get; set; } //don't forget to notify
//....
}
And finaly you can bind your CurrentPage to your Frame
<Frame Content="{Binding CurrentPage}"/>
Using MVVMC
Using a Locator

Related

How to rename an item in a ListBox? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want the user to be able to directly rename an item in a ListBox with the same effect as we can see in the file explorer of Windows by example. Like this:
Is there a simple way to achieve this?
Thanks for your answers.
Assuming your item class has a read/write property that is displayed in the ListBox, e.g.
public class MyItem
{
public string ItemText { get; set; }
}
you could bind the Text property of a TextBox in the ItemTemplate of the ListBox to that property:
<ListBox x:Name="ListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding ItemText}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In order to have the ItemText property updated while you type into the TextBox (in contrast to when the TextBox loses focus), you would have to set the Binding's UpdateSourceTrigger:
<TextBox Text="{Binding ItemText, UpdateSourceTrigger=PropertyChanged}"/>

WPF - Best code architecture for handling numerous tab items in Mainwindow [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to create a WPF application where the homepage (aka MainWindow) will contain several tabs.
I'm wondering what would be the best approach/architecture to encapsulate code specific to each tab instead of coding everything in the same place ?
NOTE
After some research I found UserControl class, but I've always thought that class was use in order to avoid re-coding the same logic over and over. In my case I would be using them once for every tab.
Depending on your preference and desired level of complexity, you can use MVVM or just create a UserControl for every tab to better distribute the code. Resulting in your window's xaml looking something like:
<TabItem x:Name="tab_peerView" Header="Peers (Lobby)" Visibility="Visible">
<Views:SomeUserControl1 />
</TabItem>
<TabItem x:Name="tab_PrepCheckList" Header="Prep" Visibility="Visible">
<Views:SomeUserControl2 x:Name="view_prep" />
</TabItem>
Doing this method you can also offload things like command bindings and respective Execute and CanExecute to the UserControls.

How create Dynamic Resource for Entry and Label text values in Xamarin.Forms VS2015 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to save the value entered through an Entry to then be shown on a label?
All the examples I find only show cosmetic factors in the application.
You can bind both the Entry and your Label to the same Viewmodel property.
XAML :
<Entry Text={Binding MyText} />
<Label Text={Binding MyText} />
Viewmodel :
private string _myText;
public string MyText
{
get
{
return _myText;
}
set
{
_myText = value;
OnPropertyChanged();
}
}
You can find more samples at Interactive_MVVM
Or you can do a View to View Binding :
<Entry x:Name="Entry1" Text="{Binding Country}"/>
<Label BindingContext="{x:Reference Entry1}" Text="{Binding BindingContext.Country}"/>
You can find more samples at View-to-View Bindings

Using WPF & C#, how to show uploaded files as Hyperlinks with a delete button for each file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Using WPF & C#, how to show uploaded files as Hyperlinks, giving a delete button for each file, so that we can delete one file at a time if not required. I am able to show the files as mere text in the textbox. How to proceed further. Can anybody help me please.??
Well you are going to need to enumerate the files so start with IEnumerable<string> System.IO.Directory.EnumerateFiles(string path).
This of course will need to be exposed to the View so that you can create the UI for each item. This is done as a list
<ListBox ItemsSource="{Binding ListOfFiles}">
<ListBox.ItemTemplate>
<DataTemplate>
To create a hyperlink you simply use a hyperlink inside a textblock such as
<TextBlock>
<Hyperlink Command="{Binding DeleteCommand}" CommandParameter="{Binding}" Text="{Binding}" />
</TextBlock>
You will notice that I'm binding to a DeleteCommand which expects to have a pararmeter passed to it, that parameter is the same as the text displayed in the link. I'd choose this over trying to do it with a Clicked since you get the filename.
How you implement the Command is up to you and whether there is a default Command implementation in whichever MVVM framework you want to use. If you aren't using one then you can use the RelayCommand from Josh Smith's original MVVM article
RelayCommand _deleteCommand;
public ICommand DeleteCommand
{
get
{
if (_deleteCommand == null)
{
_deleteCommand = new RelayCommand(
param => this.Delete(param),
param => this.CanDelete(param));
}
return _deleteCommand;
}
}
You then only need to provide implementations for bool CanDelete(sting filename) which maps directly to System.IO.File.Exists(filename) and for void Delete(string filename) which maps to System.IO.File.Delete(filename).

WPF C# set trigger property in control by code behind [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
<ContentControl Width="130"
Height="130"
Canvas.Top="60"
Canvas.Left="50"
***Selector.IsSelected="True"***
Style="{StaticResource DesignerItemStyle}">
I would like to set property Selector.IsSelected to ContentControl by using code behind. But I don't know how to do it. Please help me and give me some example.
If you want to set an attached dependency property in code you do this
ContentControl x;
//To set the value
x.SetValue(Selector.IsSelectedProperty, true);
//To Clear the value
x.ClearValue(Selector.IsSelectedProperty);
//Set using the static function on Selector
Selector.SetIsSelected(x, true);
For accessing a control in Code-behind you need first provide it a name -
<ContentControl
x:Name=""ContentControl1"
Width="130"
Height="130"
Canvas.Top="60"
Canvas.Left="50"
***Selector.IsSelected="True"***
Style="{StaticResource DesignerItemStyle}">
and then you can access it in code and set the value as mentioned in other answer -
ContentControl1.SetValue(Selector.IsSelectedProperty, true);
Apart from this it would be a good idea to look at creating a property in code-behind or ViewModel(MVVM) and bind that directly to your control like this -
<ContentControl
Width="130"
Height="130"
Canvas.Top="60"
Canvas.Left="50"
Selector.IsSelected="{Binding IsSelectedBoolProperty, Mode=OneWay}"
Style="{StaticResource DesignerItemStyle}">
This techniques will be very useful in case you have a lot of controls in your window and I would suggest you to look at implementing MVVM in your application to avoind doing these kind of things in code-behind.

Categories

Resources