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}"/>
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a list of strings in the main window class and two ItemsControl binded to same list and SelectedItem dependency property.
What i did i used Button itemtemplate for the 2nd ItemsControl and on Button Click event i am able to get the value of the selected button and then pass it to SelectedItem property and SelectedItem property is binded to the textbox.
What i want to do now is to Highlight the same item in 1st ItemsContol as well and also want backward selection.
Note: Need to use pure ItemsControl only. No other control like ListBox etc. are allowed. Also i have been asked to use ItemsControl class descendant for SelectedItem which i don't understand.
Thanks.
Since you are referring to SelectedItem I assume that you are using a Selector derived control, instead of a pure ItemsControl.
In this case a simple solution is to set Selector.IsSynchronizedWithCurrentItem to True on both controls and bind the TextBoxto either one of the SelectedItem properties (or to the common binding source property of SelectedItem):
<StackPanel>
<TextBox Text="{Binding ElementName=ListBox, Path=SelectedItem}" />
<ListBox x:Name="ListBox"
IsSynchronizedWithCurrentItem="True" />
<ListBox IsSynchronizedWithCurrentItem="True" />
</StackPanel>
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
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
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I made a list and populated it with the file urls, I want to grab that url from the text block that is in the list inside of that data template but the function that I made for that purpose isn't returning me expected string. It is giving me null instead. The function I used was SelectionChanged property of the ListView
<ListView ItemsSources="{x:Bind noticeData}"
SelectedIndex="{x:Bind MasterListView.SelectedIndex, Mode=OneWay}"
x:Name="uriList" SelectionChanged="uriList_SelectionChanged"
IsItemClickEnabled="True" Grid.Row="3">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Datum_2">
<StackPanel>
<TextBlock x:Name="downFileUri" Grid.Row="2" Grid.Column="0"
VerticalAlignment="Center" HorizontalAlignment="Center"
Margin="0,0,0,20" Visibility="Visible"
Text="{x:Bind file_url}" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When following the MVVM pattern you will more often than not, skip using control event handlers, such as click events and selection events. Instead, the pattern allows us to bind to properties and capture when those property's change inside the properties setting block.
Instead of binding to the SelectedIndex you want to bind to the SelectedItem
The SelectedItem will be of whatever type your collection is holding. In the below example I assume you have an object called NoticeData
<ListBox SelectedItem="{Binding MySelectedItem, Mode=TwoWay}">
Then in your view model
private NoticeData _mySelectedItem;
public NoticeData MySelectedItem
{
get{ return _mySelectedItem; }
set{ _mySelectedItem = value;}
}
Here is the MSDN documentation on ListBox as you will see there is both SelectedItem and SelectedItems properties. These can both be bound to. SelectedItems of course is used if you enable multiple selection.
Something else you will likely need to consider is implementing INotifyPropertyChanged on your ViewModel if you haven't already.
Here is the MSDN documentation for that as well
Edit based on comments
There is an alternative method that you can use for binding as well.
If you wish to bind another element to your selected item you just need to name your element.
<ListBox x:Name="MyListBox" ItemsSource="{Binding myItems}"/>
<TextBlock Text="{Binding ElementName=MyListBox, Path=SelectedItem.WhatEverProperty}"/>
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.