I have small problem.
Code behind:
...
public struct Project
{
string Name;
string Path;
public Project(string Name, string Path = "")
{
this.Name = Name;
this.Path = Path;
}
}
...
Resources code:
<DataTemplate x:Key="ItemProjectTemplate">
<StackPanel>
<Image Source="Assets/project.png" Width="50" Height="50" />
<TextBlock FontSize="22" Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
Normal code in Grid:
<ListView Grid.Column="1" HorizontalAlignment="Left" Height="511"
Margin="25,72,0,0" Grid.Row="1" VerticalAlignment="Top" Width="423"
x:Name="Projects" ItemTemplate="{StaticResource ItemProjectTemplate}" />
I have no problem with ListView source, which is being set in C# code, and also my template is being loaded. But this happens when I'm running my application:
As you can see the project name (Project.Name
) is not being displayed, but in my ListView template is data binding, so it should work. Does anybody know why is my data binding for a text not working? Please help.
DataBinding
works for Properties
and not on Fields
. Replace Struct with class and make your fields as properties-
public class Project
{
public string Name {get; set;}
public string Path {get; set;}
public Project(string Name, string Path = "")
{
this.Name = Name;
this.Path = Path;
}
}
You have to bind against public properties! And use classes instead of structs. Structs are passed by value to the GUI and hence, you can't make changes on the actual source-items.
public class Project
{
public string Name{ get; set;}
public string Path{ get; set;}
public Project(string Name, string Path = "")
{
this.Name = Name;
this.Path = Path;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.