I am using MVVM, I am trying to bind three TextBox's to a Client class properties as follow:
<TextBox Text="{Binding NewClient.Name, Mode=OneWayToSource}" Grid.Column="1" HorizontalAlignment="Left" Margin="5"/>
<TextBox Text="{Binding NewClient.NameInLatin, Mode=OneWayToSource}" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Margin="5"/>
<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="1" >
<TextBox Text="{Binding NewClient.IDNumber, Mode=OneWayToSource}" Margin="5" />
<Button Content="{Binding ScanLabel,Source={StaticResource LocalStrings}}" Margin="4"/>
</StackPanel>
In my View Model I defined the NewClient property the classic way:
private Client newClient;
public Client NewClient
{
get { return newClient; }
set
{
newClient = value;
NotifyPropertyChanged("NewClient");
}
}
When place a breakpoint inside a boolean property just to test the value of newClient, and I find it null.
So why is it the newClient property loses it's value?
Here is the entire xaml part:
<Border Background="AntiqueWhite"
DataContext="{StaticResource ServicesViewModel}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding NameLabel, Source={StaticResource LocalStrings}}"
Style="{StaticResource SubTitles}" />
<TextBlock Text="{Binding NameInLatinLabel, Source={StaticResource LocalStrings}}"
Grid.Row="1"
Style="{StaticResource SubTitles}" />
<TextBlock Text="{Binding IDNumberLabel, Source={StaticResource LocalStrings}}"
Grid.Row="2"
Style="{StaticResource SubTitles}" />
<TextBox Text="{Binding NewClient.Name, Mode=OneWayToSource}"
Grid.Column="1"
HorizontalAlignment="Left" />
<TextBox Text="{Binding NewClient.NameInLatin, Mode=OneWayToSource}"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left" />
<StackPanel Orientation="Horizontal"
Grid.Row="2"
Grid.Column="1">
<TextBox Text="{Binding NewClient.IDNumber, Mode=OneWayToSource}" />
<Button Content="{Binding ScanLabel,Source={StaticResource LocalStrings}}" />
</StackPanel>
</Grid>
</Border>
I tired to bind to a property I created called Name, and the binding did work.
By default TextBox.Text updates when a control loses focus, so if you need to update your property every time text changes you should use binding like this:
{Binding NewClient.Name, UpdateSourceTrigger=PropertyChanged}
How to: Control When the TextBox Text Updates the Source
If it still doesn't work, then probably NewClient is not initalized. In Debug Output window should show you issues with bindings.
You don't need to use the name of the item held in the datacontext. Simply report its property only:
Before:
Binding NewClient.Name, Mode=OneWayToSource
after
Binding Name, Mode=OneWayToSource
Also OneWayToSource is primarily used for read-only data. Remove that to
Binding Name
I was experiencing similar problem. In ViewModel I did binding to a Class property.
class SampleClass
{
public string Name {get; set;}
public int Age {get; set;}
}
class ViewModelClass
{
public SampleClass Sample {get; set;}
}
And I found thhat for binding to work, should be used Properties in SampleClass, not Fields. It helped me.
Related
Referencing this example:
https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-bind-to-a-collection-and-display-information-based-on-selection
(some relevant code snippets:)
<Window.Resources>
<local:People x:Key="MyFriends"></local:People>
<DataTemplate x:Key="DetailTemplate">
<Border Width="300" Height="100" Margin="20"
BorderBrush="Aqua" BorderThickness="1" Padding="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="First Name:"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=FirstName}"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Last Name:"/>
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=LastName}"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Home Town:"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=HomeTown}"/>
</Grid>
</Border>
</DataTemplate>
</Window.Resources>
<ListBox Width="200" IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource MyFriends}}"/>
<ContentControl x:Name="contentControl1"
Content="{Binding Source={StaticResource MyFriends}}"
ContentTemplate="{StaticResource DetailTemplate}" />
Both ListBox.ItemsSource and ContentControl.Content bind to the same source (MyFriends, an instance of the People class which derives from ObservableCollection<Person>). If my understanding is correct, this means that both the ListBox.ItemsSource and ContentControl.Content properties will be bound to the same implicitly created instance of ListCollectionView.
I understand that setting ListBox.IsSynchronizedWithCurrentItem="True" synchronizes ListBox.SelectedItem and ItemCollection.CurrentItem.
DetailTemplate (above) displays the details of the selected ListBox item, despite being 'bound' to a ListCollectionView. Specifying Path=/ (what I thought would be necessary to achieve the resulting behavior) does not have any effect - it's as if WPF knows to do it implicitly somehow:
<ContentControl x:Name="contentControl1"
Content="{Binding Source={StaticResource MyFriends}, Path=/}"
ContentTemplate="{StaticResource DetailTemplate}" />
As a test, I created another ContentControl with Content bound to a DataTemplate containing a ListBox:
<ContentControl x:Name="contentControl2"
Content="{Binding Source={StaticResource MyFriends}}"
ContentTemplate="{StaticResource DetailTemplate2}" />
<DataTemplate x:Key="DetailTemplate2">
<ListBox ItemsSource="{Binding}"></>
</DataTemplate>
And it displayed the list.
My question is: Why does DataTemplate get the selected Person object while the ListBox and DetailTemplate2 get the People collection?
(the behavior is desirable, I just don't understand what black magic is occurring under the hood to make it so)
Is a good question! I didn't notice that until read your post. So, after did some digging from source code of PropertyPathWorker, it appears that when PropertyPathWorker failed to solve a member of an object, in your case, it try to solve 'FirstName', 'LastName' ect. with 'MyFriends', it will try to solve it with the view of the object. And if still failed, it will try to solve it with view's CurrentItem, and that's where the magic happened. You can find those codes in PropertyPathWorker.UpdateSourceValueState(int k, ICollectionView collectionView, object newValue, bool isASubPropertyChange) and PropertyPathWorker.ReplaceItem(int k, object newO, object parent).
I work on a project target on Windows Phone 7.5 and above.
What I have
ListBox
<ListBox HorizontalAlignment="Left"
VerticalAlignment="Top"
SelectedItem="{Binding singleFavListItem, Mode=TwoWay}"
ItemTemplate="{StaticResource userFavBoardListItemTemplate}"
ItemsSource="{Binding userfavboardlist}"
ScrollViewer.VerticalScrollBarVisibility="Disabled" Margin="12,0,0,12"/>
ItemTemplate
<DataTemplate x:Key="userFavBoardListItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left"
TextWrapping="Wrap"
Text="{Binding boardName}"
VerticalAlignment="Center"
FontSize="{StaticResource PhoneFontSizeMedium}"
Foreground="{StaticResource TitleColor}"/>
<Button Command="{Binding quitBoardCommand}"
CommandParameter="{Binding boardUrl}"
Content="Quit"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Column="1"
FontSize="{StaticResource PhoneFontSizeSmall}"
BorderBrush="{StaticResource DateArticalCategoryColor}"
Foreground="{StaticResource DateArticalCategoryColor}">
</Button>
</Grid>
</DataTemplate>
ViewModel
public MyFavListViewModel()
{
this._quitBoardCommand = new DelegateCommand(this.quitBoardAction);
}
private ICommand _quitBoardCommand;
public ICommand quitBoardCommand
{
get
{
return this._quitBoardCommand;
}
}
private void quitBoardAction(object p)
{
//my business logic here
}
Error
I found a error in the OutPut windows:
'xicihutong.Model.UserFavBoardListRawData' (HashCode=55845053).
BindingExpression: Path='quitBoardCommand'
DataItem='xicihutong.Model.UserFavBoardListRawData'
(HashCode=55845053); target element is
'System.Windows.Controls.Button' (Name=''); target property is
'Command' (type 'System.Windows.Input.ICommand')..
What's the problem
What confuse me is that the quitBoardCommand never get triggered when I tap the button? It seems that I can't bind the Command to the button, the DelegateCommand part is right, because I can use it to bind command in other pages. And the SelectedItem of the ListBox works right, also.
Why I can't bind this one?
You need to reference the DataContext of your ListBox to bind to your command. To fix this, give your ListBox a name then reference the command property
<ListBox x:Name="myLB"
<!-- rest of your stuff -->
/>
<Button
Command="{Binding Path=DataContext.quitBoardCommand, ElementName=myLB}"
CommandParameter="{Binding boardUrl}"
Content="Quit" />
Can you try this code. You don't forget to change "datacontext" name.
<DataTemplate x:Key="userFavBoardListItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<TextBlock HorizontalAlignment="Left"
TextWrapping="Wrap"
Text="{Binding boardName}"
VerticalAlignment="Center"
FontSize="{StaticResource PhoneFontSizeMedium}"
Foreground="{StaticResource TitleColor}"/>
<Button Command="{Path=quitBoardCommand,Source={StaticResource datacontext}}"
CommandParameter="{Binding boardUrl}"
Content="Quit"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Grid.Column="1"
FontSize="{StaticResource PhoneFontSizeSmall}"
BorderBrush="{StaticResource DateArticalCategoryColor}"
Foreground="{StaticResource DateArticalCategoryColor}">
</Button>
</Grid>
</DataTemplate>
Hei,
I'm using MVVM with my wpf application. I have DataGrid what shows list of items and i have binded SelectedItem to CurrentSequence propery in my ViewModel. When CurrentSequence changes properties of that object are shown in other controls for editing.
Heres my xaml:
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=ColorSettingsSequences}"
SelectedItem="{Binding Path=CurrentSequence, Mode=TwoWay}">
.... more things here ...
</DataGrid>
<StackPanel Grid.Column="0" Grid.Row="0">
<Grid>
<Label Content="Start temperature (°C)" Height="28" HorizontalAlignment="Left" x:Name="lblSeqStartTemp" VerticalAlignment="Top" />
<TextBox Height="23" Margin="0,28,10,0" x:Name="tbSeqStartTemp" VerticalAlignment="Top" Text="{Binding Path=CurrentSequence.StartTemp}" />
</Grid>
<Grid>
<Label Content="Start color" Height="28" HorizontalAlignment="Left" x:Name="lblSeqHue" VerticalAlignment="Top" />
<xctk:ColorPicker Margin="0,28,10,0" x:Name="clrpSeqHue" SelectedColor="{Binding Path=CurrentSequence.StartHue, Converter={StaticResource hueToColor}, ConverterParameter=False}" ShowStandardColors="False" />
</Grid>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="0">
<Grid>
<Label Content="End temperature (°C)" Height="28" HorizontalAlignment="Left" x:Name="lblSeqEndTemp" VerticalAlignment="Top" />
<TextBox Height="23" Margin="0,28,10,0" x:Name="tbSeqEndTemp" VerticalAlignment="Top" Text="{Binding Path=CurrentSequence.EndTemp}" />
</Grid>
<Grid>
<Label Content="End color" Height="28" HorizontalAlignment="Left" x:Name="lblSeqEndHue" VerticalAlignment="Top" />
<xctk:ColorPicker Margin="0,28,10,0" x:Name="clrpSeqEndHue" SelectedColor="{Binding Path=CurrentSequence.EndHue, Converter={StaticResource hueToColor}, ConverterParameter=False}" ShowStandardColors="False" />
</Grid>
</StackPanel>
Code:
private ColorSettingsSequencesSequence _currentSequence;
public ColorSettingsSequencesSequence CurrentSequence
{
get
{
return this._currentSequence;
}
set
{
this._currentSequence = value;
OnPropertyChanged("CurrentSequence");
}
}
Now the questions is, how could i add a button what would save changes or add a totally new item. I know how to do one or another, but what about 2 together with same controls...
You can create a property on your view model which is of type ColorSettingsSequencesSequence named CurrentSequenceEdit and you can set the value of this object upon changing selection.
Then upon click of an add button you can create a new ColorSettingsSequencesSequence object and set it to the value of CurrentSequenceEdit.
Typically on a new object you'll have an ID that is not set or is set to a negative number which you can use in the Save command to determine whether it needs added to the grid or not.
I am trying to bind an image on the main window with a string (stored in another class) which represents the file path of the image I want to display.
But nothing shows up.
Here is my main window code xaml code:
<HierarchicalDataTemplate x:Key="categoryTemplate"
ItemsSource="{Binding Path=Items}"
ItemTemplate="{StaticResource animalTemplate}">
<Grid MouseEnter="DockPanel_MouseEnter" MouseLeave="DockPanel_MouseLeave">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="30" />
<ColumnDefinition Width="16" />
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Center" Source="{Binding Path=IconFilePath}" VerticalAlignment="Center" Width="16" Height="16" Grid.Column="0" />
<TextBlock Text="{Binding Path=Name}" Margin="5,0,0,0" FontWeight="Bold" FlowDirection="{Binding Path=FlowDirection}" FontSize="14" HorizontalAlignment="Stretch" Grid.Column="1" />
<Border CornerRadius="2" Background="Lavender" Grid.Column="2" Margin="0,0,5,0">
<TextBlock Text="30" Foreground="DodgerBlue" HorizontalAlignment="Center" FontWeight="Bold" FontSize="13" />
</Border>
<aea:MenuButton Margin="0,0,2,0" Opacity="0" HorizontalAlignment="Right" Grid.Column="3" SnapsToDevicePixels="False" Width="16" Height="16" DisplayStyle="Text" IsEnabled="True" IsDropDownOpen="False">
<aea:SplitButtonItem IsSelected="True" Visibility="Collapsed">
<Image HorizontalAlignment="Center" Source="Assets\FeedMenu.png" VerticalAlignment="Center"/>
</aea:SplitButtonItem>
<aea:SplitButtonItem Tag="{Binding Path=me}" Selected="Subscription_MarkAllAsRead">Mark all as Read</aea:SplitButtonItem>
<aea:SplitButtonItem Tag="{Binding Path=me}" Selected="Subscription_AddAllToFavorites">Add all to Favorites</aea:SplitButtonItem>
<aea:SplitButtonItem Tag="{Binding Path=me}" Selected="Subscription_ReadAllLater">Read all Later</aea:SplitButtonItem>
<aea:SplitButtonItem Tag="{Binding Path=me}" Selected="Subscription_OpenAllBrowser">Open all in browser</aea:SplitButtonItem>
</aea:MenuButton>
</Grid>
<!--<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>-->
</HierarchicalDataTemplate>
Here is my other class:
public string IconFilePath { get; private set; }
public Subscription()
{
this.IconFilePath = #"C:\Users\Din\Din\Programming\Webs\Ended Projects\CodeCaged\Products\Read 360\Read 360\Read 360\bin\Release\feeds\1.ico";
}
You are binding relative to the DataContext so you need to make sure its an instance of that class. Also check for binding errors, not more to be said with this little context.
It is hard without a full code listing of how this control is setup (e.g. where and how is the DataContext set?, and how is the list of 'Items' populated?)
But on the surface it appears you're expecting to get both 'Name' and 'IconFilePath' from an Items element, so to confirm the Subscription Class defines both IconFilePath and Name?
A tool like Snoop can automatically display binding errors in a running applications visual tree; and I would expect it to list such in this case.
Also to reduce possible headaches (and this may well be the issue) it may be worth mentioning INotifyPropertyChanged for your data class. Property changes on your data class will not automatically progate otherwise.
I have a custom ItemTemplate for a ListBox and I need to "bind" a TextBlock to some special method / property.
My ListBox Source is an ObservableCollection<SearchResultItem>. SearchResultItem containing some properties.
The text need to change based on the value of another object. E.G if this object equals "foo" I need the text value to call the method GetProperty("foo") on the SearchResultItem to get the correct value.
Here is a sample of code:
<DataTemplate>
..
//Here is a Label bound to the Date Property of the SearchResultItem
<Label Margin="2,2,2,0" Grid.Row="0" Grid.Column="2" Content="{Binding Path=Date}" HorizontalAlignment="Right" HorizontalContentAlignment="Right" />
//Here is the textblock that needs to call the method with the parameter based on the value of the other object.
<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis" Grid.Row="0" Grid.Column="1" Text="I need some help there" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
..
</DataTemplate>
Do you have any idea on how to do that or a better way to do it?
Edit:
-Let's assume SearchResultItem comes from an external library and only exposes the GetProperty method.
-Let's say the "foo" value comes from ConfigurationManager.AppSettings["propertyName"]; if it helps.
OK, because your properties never change, here's one solution. You can do this via another property on your SearchResultItem class:
public string MyTextBinding
{
get
{
return myDictionary.ContainsKey("foo") ? return myDictionary["foo"] : return "myDictionary doesn't contain foo";
}
}
Then just bind your textbox to this property:
<DataTemplate>
<Label Margin="2,2,2,0" Grid.Row="0" Grid.Column="2" Content="{Binding Path=Date}" HorizontalAlignment="Right" HorizontalContentAlignment="Right" />
<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis" Grid.Row="0" Grid.Column="1" Text="{Binding Path=MyTextBinding, Mode=OneWay}" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>
</DataTemplate>
Just use a IValueConverter that takes a SearchResultItem and return the expected text
<TextBlock Margin="2,2,2,0" TextTrimming="CharacterEllipsis"
Grid.Row="0" Grid.Column="1"
Text="{Binding Path=.,
Converter={StaticResource PropertyValueFromSearchResultItemConverter},
ConverterParameter=Foo}"
HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black"/>