|
|
Hello,
I used the following XAML code:
<StackPanel x:Name="grd">
<ListView x:Name="listView">
<ListView.View>
<GridView>
<GridViewColumn Header="Severity" DisplayMemberBinding="{Binding Icon}"/>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Immagine" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding ImagePath}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</Window>
And the following C# code:
public partial class MainWindow : Window
{
public class MyItem
{
public string Icon { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string ImagePath { get; set; }
}
public MainWindow()
{
InitializeComponent();
MyItem Item = new MyItem();
Item.Icon = "1";
Item.Id = 0;
Item.Name = "Zvika";
Item.ImagePath = "icon1.png";
this.listView.Items.Add(Item);
}
}
I also added the file icon.png to the project.
Now it works.
Regards,
Z.V
modified 30-Dec-15 21:44pm.
|
|
|
|
|
I know that is an aberration but I am building a TreeListView where the Tree Column [0] is fixed and the following columns are dynamically created as a collection of columns in the viewmodel. The code behind then adds the columns to the TreeListView when they are built.
The creation of the columns is done on a BGW and a message is generated for the code behind to consume in the BGW complete method.
However the Message is complaining that it is on the wrong thread.
VM BGW Complete Method
private void NavComplete(object sender, RunWorkerCompletedEventArgs e)
{
worker.DoWork -= LoadTreeStuff; ;
worker.RunWorkerCompleted -= NavComplete; ;
if (e.Result != null)
{
MessageBox.Show(e.Result.ToString());
}
BusyIndicator = false;
AppMessages.MessageObject.Send("ColumnsReady");
}
I would have thought the AppMessage.MessageObject.Send would be on the main application thread.
public static class MessageObject
{
public static void Send(object oPayload)
{
Messenger.Default.Send<object>(oPayload);<-------Errors
}
<pre>
public static void Register(object recipient, Action<object> action)
{
Messenger.Default.Register<object>(recipient, action);
}
public static void UnRegister(object recipient)
{
Messenger.Default.Unregister<object>(recipient);
}
}</pre>
with the following error
Quote: The calling thread cannot access this object because a different thread owns it.
Any suggestions other than getting rid of the BGW (works perfection when it is removed)
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Wrap the code that's failing in a Dispatcher.Invoke (or Dispatcher.BeginInvoke) to marshall the code back to the correct thread.
This space for rent
|
|
|
|
|
I'm learning WPF. Over the past couple of days, I've worked my way through one-way binding, two-way binding, and list binding. For each of these, I created my own model classes to hold the data displayed on the window.
Now I'm tackling Entity Framework. Instead of using my own model classes, I want to use a data model created by the Entity Framework designer. I just tried it and it works beautifully. Even though it works, however, there is one thing I don't understand: Why does Visual Studio think I need a database?
For my simple test project, I'm not using any persistent data, and therefore I don't need a database. In my code, I create a Data Model Container and interact only with the in-memory version of the data (which I believe is done by specifying "Local" whenever referring to a table). Even though I don't need a database, Visual Studio kept giving me warnings related to the "Table Mapping" until I created one. After I created a database and connection string, everything worked properly with no errors or warnings.
As a test, I then deleted the database and changed the connection string in App.config to something bogus. Everything continued to work properly, indicating that the database and connection string are unused. It is only when I delete the connection string completely that I get errors again.
So my question is this: Why is Visual Studio insisting that I have a connection string which it obviously isn't using?
-TC
PS: I suppose this question is about Entity Framework more than WPF, but I came to the question through my research on WPF, so I hope it is appropriate for this forum. If not, let me know and I'll gladly repost elsewhere.
|
|
|
|
|
Caveat - I am extremely prejudiced...
Entityframework is the work of satan, it wraps up your data management and ORM into a black box and then feeds you the nice, sanitised data titbits. It also tries to do everything for everyone and is therefore a monster.
You end up with a programmer who knows how to use EF but does not understand the underlying data management or how the ORM works and how he got his sanitised object. When it does go wrong they will try and work around it withing the EF structure, not understanding the fundamental problem that caused the error!
If you do not need a database (you will for your production system) are yo even contemplating including EF in you project.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: Entityframework is the work of satan, it wraps up your data management and ORM into a black box and then feeds you the nice, sanitised data titbits. It also tries to do everything for everyone and is therefore a monster.
You end up with a programmer who knows how to use EF but does not understand the underlying data management or how the ORM works and how he got his sanitised object. When it does go wrong they will try and work around it withing the EF structure, not understanding the fundamental problem that caused the error!
Thank you for that advice. I am wary of technologies that obscure, rather than clarify, what is going on behind the scenes. From your comments, it sounds like Entity Framework is one such technology, so I'll be careful.
Mycroft Holmes wrote: If you do not need a database (you will for your production system) are yo even contemplating including EF in you project.
I'm not sure I understand the question. Are you implying that Entity Framework only merits contemplation when a database is required?
-TC
|
|
|
|
|
_T_C_ wrote: Are you implying that Entity Framework only merits contemplation when a database is required? AFAIK that is EFs primary functionality, model and manage the movement of data from the store to the business layer. Remembering that my knowledge of EF is pretty shallow
Most of us will have built our own ORM and data access layer, some predating EF by some years, mine started out in the mid 90s!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
The connection string does more than specify a database: it identifies an EF compatible data provider (which could be based on Excel for that matter).
I suspect that if you tried to "save" your entities, you would get an error if the connection string is invalid and / or the database is missing; EF is not using the connection (string) until it needs it.
There is more than one way to supply a connection (string) to a dbContext; the app.config file is the default. If you created the dbContext with an invalid string in the constructor, that would probably work too for the time being.
(All just speculation since I tend to use the "typical" case).
|
|
|
|
|
Mr. Holmes and Mr. Schmitz,
Thank you for the replies. I appreciate the information you've provided, but I must be failing to grasp some key concept because Entity Framework's behavior in this respect still seems like a mystery to me. In an attempt to get at the answer from a different direction, let me turn the question around and ask it this way: If you had to code an application which generates a few megabytes of relational data, with no requirement to save those data, what technology would you use?
I would expect that to be a common scenario. A lot of extract/transform/load applications would probably fit that description. Let me offer a specific example: I need to read lab data from a few data sources, then filter, transform, and analyze the data, then offer a few charts & reports and the ability to export the results in various formats. What technologies would be useful for such a project?
To my uninformed eye, Entity Framework seems a likely candidate. With Entity Framework, I can define the data model for my generated data using an intuitive GUI, and Entity Designer will write code to implement all the entities, relationships, and constraints for me. That will save me some time and enforce some consistency. Better yet, my data model will be self-documented in a relationship diagram, right there in the Visual Studio project, which is far better than any alternative I can think of. Why wouldn't I use Entity Framework for that reason alone? Even though I'm not using Entity Framework's ORM capabilities, those are a bonus too, because I have an easy migration path if the project ever evolves into something that requires persistent data. Furthermore, it means the data modeling syntax for this project will be the same as the data modeling syntax for other projects which require Entity Framework's ORM capabilities, lessening the overall learning curve for my team.
But if Entity Framework is really appropriate for this scenario, then why is it demanding a connection string? That one detail has made me suspicious that I am missing something which makes Entity framework wholly inappropriate here. I feel that I need to keep digging until I figure it out.
Please let me know if you have any further insight. In any case, I thank you for the advice you've provided thus far.
-TC
|
|
|
|
|
(IMO) EF "assumes" at some point you are going to serialize or retrieve your data to / from an "external" source; for that it needs a "connection string". (The alternative is to abort later on).
Anyway, EF 7.0 now has support for an "in-memory" database.
I suspect that was "cleaner" to implement than the alternatives.
|
|
|
|
|
I'm learning WPF. Yesterday, I was stumped by basic binding, and Gerry Schmitz gave me the push I needed to understand DataContext. Today, I've been challenging myself with progressively more difficult binding tasks.
Now I'm stumped by this one: I have a window with a list box showing chart names. When the user selects a chart on the list box, I want that chart's name and description to appear on the window in text boxes with two-way binding. This image illustrates what I'm talking about:
http://i.imgur.com/63ndQsz.png[^]
I've pasted the XAML code for my ChartsWindow below. All suggestions are appreciated.
-TC
<Window x:Class="TestProject.Charts.ChartsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:testProjectCharts="clr-namespace:TestProject.Charts"
mc:Ignorable="d"
d:DataContext="{x:Static testProjectCharts:SampleModels.SampleChartsModel}"
Title="ChartsWindow" Height="300" Width="600">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Chart" Margin="6,6,6,0" />
<ListBox Grid.Row="1" Name="NavigationListBox" VerticalAlignment="Stretch" Width="100" Margin="6,0,6,6" ItemsSource="{Binding Charts}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ChartName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="6,6,6,0" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="TextBox">
<Setter Property="Margin" Value="6,6,6,0" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Grid.Resources>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Chart ID:" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="[Bind to the selected chart's ChartId here]" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Chart Name:" />
<TextBox Grid.Row="1" Grid.Column="1" Text="[Bind to the selected chart's ChartName here]" />
<TextBlock Grid.Row="2" Grid.Column="0" Text="Chart Description:" />
<TextBox Grid.Row="2" Grid.Column="1" Text="[Bind to the selected chart's ChartDescription here]" />
</Grid>
</Grid>
</Window>
|
|
|
|
|
I can answer my own question. I just need to replace the XAML for the three bound fields to these three lines respectively:
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding ElementName=NavigationListBox, Path=SelectedValue.(testProjectCharts:Chart.ChartId)}" />
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding ElementName=NavigationListBox, Path=SelectedValue.(testProjectCharts:Chart.ChartName)}" />
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding ElementName=NavigationListBox, Path=SelectedValue.(testProjectCharts:Chart.ChartDescription)}" />
WPF is getting easier as I learn the syntax, but the learning curve for the syntax is extremely harsh.
-TC
|
|
|
|
|
I'd be inclined to simplify that by binding the inner grid's DataContext to the selected item from the list:
<Grid Grid.Column="1" DataContext="{Binding ElementName=NavigationListBox, Path=SelectedValue}">
...
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=ChartId}" />
...
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=ChartName}" />
...
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=ChartDescription}" />
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm having trouble understanding WPF. I've read many tutorials and reviewed many examples, but I still can't seem to grasp the basics. I'm hoping someone here can give me the push I need to get on track.
A little background: I manage a software product for my company. I'm an engineer, not a programmer, so I program the math stuff and delegate everything else. The product uses WinForms now, but the UI is due for an overhaul and I'm considering a switch to WPF. Thus, I'm trying to learn WPF well enough to plan a possible WinForms to WPF transition.
I've given myself this agenda:
1) As a warm-up exercise, create a WPF version of the About form which uses MVP, MVVM, or some such design pattern to display the application revision number as read from an About Model.
2) Make the About Window show a sample revision number when in design mode.
3) Change the revision number label into a text box that can write to the About Model, just to confirm that I understand two-way binding.
4) Create a WPF version of the Chart form. The Chart Window will be significantly more complicated than the About Window, since it must display a list box of chart names and fields whose values change depending on the list box selection.
I'm working on Item 1 right now. Below, I've pasted the code I've written so far. My question is: How do I change the
<TextBlock Text="v1.0.0.0" /> bit so the About Window shows the contents of its .Model.Version property when the window is displayed?
Here is my startup code:
using System;
namespace TestProject
{
public static class Startup
{
[STAThread]
public static void Main()
{
AboutModel aboutModel = new AboutModel { Version = "v1.2.3.4" };
AboutWindow aboutWindow = new AboutWindow(aboutModel);
aboutWindow.ShowDialog();
}
}
}
Here is my About Model:
namespace TestProject
{
public class AboutModel
{
public string Version { get; set; }
}
}
Here is my About Window XAML:
<Window x:Class="TestProject.AboutWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="About" Height="200" Width="300">
<StackPanel>
<TextBlock Text="Product Name " />
<TextBlock Text="v1.0.0.0" />
</StackPanel>
</Window>
And here is my About Window C# code:
namespace TestProject
{
public partial class AboutWindow
{
public AboutWindow() : this(new AboutModel()) { }
public AboutWindow(AboutModel model)
{
InitializeComponent();
_model = model;
}
private readonly AboutModel _model;
public AboutModel Model => _model;
}
}
|
|
|
|
|
A WPF "window" runs in the context of a WPF "application".
Start off right: use a WPF application "project" (created in Visual Studio) to get your coding off to the right start with a working application (and window) that you can modify / extend.
Visual Studio Community Edition 2015 is powerful and free.
|
|
|
|
|
Mr. Schmitz,
Thank you for the reply.
To clarify: I am using Visual Studio Community Edition 2015, and this is a WPF Application project. My AboutWindow is a working copy of the initial window. I seek to extend it by binding that one TextBlock to the AboutModel's Version property.
Did you mean to imply that I have not started off right? If I have not started off right, can you be more specific about what I have done wrong?
|
|
|
|
|
Is your app currently "running"?
The code you show is not enough to be able to tell if this is working code; e.g. there are missing using statements.
And your start-up code is not "typical".
|
|
|
|
|
Gerry Schmitz wrote: Is your app currently "running"?
Yes
Gerry Schmitz wrote: The code you show is not enough to be able to tell if this is working code; e.g. there are missing using statements.
Would it help for me to post the solution? Is there a way to do that on this forum? Regarding the using statements: I must have used Resharper to clear all unnecessary using statements. The code runs fine with just the using statements shown, though.
Gerry Schmitz wrote: And your start-up code is not "typical".
I tried to write the simplest possible code to instantiate an AboutModel and assign it to a field in the AboutWindow object before displaying the window. What is the typical approach?
|
|
|
|
|
(See my other post regarding a solution).
"Typical" means there's usually a "main window"; and dialogs, etc. run after that.
Without a "main window" an app will just exit.
For testing a dialog, I would consider a main window with a button that could invoke the dialog repeatedly (for testing and tracing purposes).
|
|
|
|
|
Okay, I follow you. I'll modify the project to start that way.
|
|
|
|
|
Try this:
<StackPanel>
<TextBlock Text="Product Name " />
<TextBlock Text="{Binding Version}" />
</StackPanel>
public AboutWindow(AboutModel model){
InitializeComponent();
_model = model;
this.DataContext = model;
}
modified 18-Dec-15 18:57pm.
|
|
|
|
|
That worked! And it was so simple. I can't tell you how many examples I've looked at today, and they are all much more complicated than that.
|
|
|
|
|
You probably struggled because you were looking at examples that define a full application rather than simple binding.
WPF is a very different beast to winforms, and binding is the glue that makes it work. When you have completed your POC and want to get into a proper structure you will need to pick a framework (MVVM Light, Prism etc) and work with that. Rolling your own framework is painful although there are a few here who have done exactly that.
We did the transition from winforms to Silverlight (subset of WPF for the web) and it was a good 3 months before we got productive and settled into MVVM Light. Once we got a good handle on the framework and added our extensions, productivity really took off.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Great!
There are a lot more aspects to "data binding" ... This is a simple case of one-way from source to target.
|
|
|
|
|