It did also appear valid to me, but when the data templates were setup with a DataType={x:Type Interfaces:IShowTree}, the template was not applied and the name of the view model was shown. A little google for "DataTemplate Interface" returns lots of results all saying that DataTemplates don't work with interfaces. The main reason seems to be classes that imlement multiple interfaces would be difficult for WPF to pick the right DataTemplate to use.
Ed Hill _5_
Posts
-
Switch WPF UI at runtime -
Getting File PathOne option that may work for you, there is a microsoft tool that converts documents into .docx format, can you run this on the server that the files are uploaded to. If this is an option and you can get it implemented there are serveral projects out there on the interwebs that show you how to convert a docx file into html, this allows it to be viewed in a browser. I'm not at my work PC at the moment but i have a smaple project on there that allows demonstrates docx files viewed in a web browser.
-
Switch WPF UI at runtimeI'm trying to allow the user to switch which DataTemplate is used for an object based on a choice by the user. At a very basic level consider an oject you have written called MyObject:
That resource Dictionary provides 3 different DataTemplates for the same object, what i want is a mechanism to let the user pick which one gets used, but in the actual situation things are a little more complex, but once i sort the right way to do the switching then i'll be able to implement that for the more complex situation.
-
Switch WPF UI at runtimeI have a set of data that can be defined by the user, this will consist of item types i provide to them, for example (Text/Date/Decimal/Address/Lookup/Group). A group allows them to put related data together, groups can be nested as deep as the user requires. Think Composite pattern. As every user is able to create their own structure how it is best displayed will also vary from user to user. I would like to be able to provide a mechanism that allows the user to select how they want their data displayed. To do this i have created a ResourceDictionary for each way of displaying the data but i've struggled to find a good way to implement switching between display methods. I have looked into switching the ResourceDictionary in code but could not find a good way to implement this. I've also thought about implementing a DataTemplateSelector and again struggled to get this working. I tried creating empty interfaces (IShowAsTree, IShowAsTabs, IShowAsGroupBoxes), implementing them all in my view that holds the data, and casting when i return the property they are bound to.
public interface IBaseView{}
public interface IShowAsTree:IBaseView{}
public interface IShowAsTabs:IBaseView{}
public interface IShowAsGroupBoxes:IBaseView{}public class DataCollection:BaseVM, IShowAsTree, IShowAsTabs, IShowAsGroupBoxes
{
//extended data class
}public class ExtenededDataView:BaseVM
{
public String[] ViewAsOptions{get{return new[] {"Tree","Tabs","GroupBoxes"}}private String \_viewAs; public String ViewAs { get {return \_viewAs;} set { \_viewAs = value; OnPropertyChanged(()=>View); } } private DataCollection \_view; public IBaseView View { get { switch(ViewAs) { case "Tree": return \_view as IShowTabs; break; //... } } }
}
This failed, should have known but DataTemplating appears to only work on classes not interfaces. This is when i decided on quite a hack for a solution, replace the interfaces with classes, and put a property in the parent class that holds the DataCollection. This works but feels wrong, so if any one can advice me of a better solution, or an area to research more it would be appreciated. Thanks to anyone who read this far, code was typed in so may well contain the odd error, but hop
-
Working with WPFOk had a little look at this, have you tried the following
For me adding the CanUserSortColumns="True" to the grid properties was enough to get sorting working, unless you need something more than visual sorting this should work for you.
-
Working with WPFI'm afraid i have no experience with sorting on Grids, for most grids in the applications i work on we use a paid for grid component that handles the sorting for us. I'll have a quick look to see if i can figure out what you are after but i'd recommend you create a new post in the WPF forum with this question.
-
WPF ListBox Questionfor your second question try this link. code taken from the link:
<Label Content="{Binding TitleWithYear}"/>
-
Adding extra parameters to eventsThis is a good solution (+5), however if the OP is working with the System.Windows.Shapes.Ellipse then they may need to look at overriding the OnManipulationDelta[^] Method so that they can use their extended ManipulationDeltaRoutedEventArgs.
-
Crossing the UI sync context with a datasourcethe code sample i gave you puts the class in charge of refreshing the data in total control, the one call made in the UI is just letting the GrowingTable class know how to perform tasks in sync with the UI thread. after that stage has been performed the UI has no more involvement. I'm glad you have found a solutino but the assumptin that the form is callint InvokeOnUI is not quite right.
-
Crossing the UI sync context with a datasourceadd this to Growing Table
public Action InvokeOnUI{get;set;}
in the AddRow method replace
this.itemTable.Rows.Add(dr);
with
if (InvokeOnUI!=null)
InvokeOnUI(()=>this.itemTable.Rows.Add(dr));
else
this.itemTable.Rows.Add(dr);and don't forget to set it up using:
ra.InvokeOnUI = action => Invoke(action);
the sample i made is as follows if it helps
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Updater ud = new Updater();
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = ud.Data;ud.InvokeOnIU = action => Invoke(action); } private void button1\_Click(object sender, EventArgs e) { Thread run = new Thread(ud.AddInBackGround); run.Start(); }
}
public class Updater
{
public Updater()
{
Data = new DataTable();
Data.Columns.Add("Column 1");
Data.Columns.Add("Column 2");
Data.Columns.Add("Column 3");
}
public Action InvokeOnIU { get; set; }public DataTable Data { get; set; } public void AddInBackGround() { if (InvokeOnIU!=null) { var toAdd = Data.NewRow(); InvokeOnIU(()=>Data.Rows.Add(toAdd)); } }
}
-
Questions For Entity Framework GurusAm no expert here but my general approach is that you create a context in a using block per action you need to perform. For example Adding an Order item to a database create a single context, add the order and order items using that context then commit the changes. This means that if you action has one part that fails then none of it will get committed, and reduces the overhead of creating the context to one per action the user takes not creating one per item that needs adding to the database. I have also use EF in a service and it performs well enough for my needs, if it speeds up your development and performs well enough then why not use it.
-
Groupbox visibility problem in WPF after debugging and changing the combobox valuehave you put a break point in to check that the SelectionChanged Event is being triggered and passed through to the event handler? If you don't set the vivibility explicitly in the PageLoad is the combo box working as expected? I'm not able to reproduce your problem here at all from the information you have provided, so include the WPF where you decalre your combo box, page load event and at least one group box. also include the code for your selection chanage event handler and page load event handler. Or send me your project via email(my profile > right click homepage >copy link location) email address is there for a short while.
-
Groupbox visibility problem in WPF after debugging and changing the combobox valueI have provided you with a sample using the code behind, but i would say that it is worth getting used to all the things that you can do with WPF in the xaml files. As with any programming there are so many ways to do things, the styles i was suggesting can be created as resources and be reused easily by the group boxes. If you are using the MVVM approach what you may be looking to do is use templates to get your UI to change depending on Properties in the ViewModel. Just as a quick untested example if you are worried about having too much xmal, this is just a different appraoch. It is worth noting that the resources can be placed in different places, and the are in scope in any elements below where they are created, also they can be kept out of the way in resource dictionarys. Hopefully one of these solutions will work for you even if it is the code behind one.
<Setter Property="Visibility" Value="Collapsed"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Three"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers> <Setter Property="Visibility" Value="Collapsed"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers> One Two Three
-
Groupbox visibility problem in WPF after debugging and changing the combobox valueif you are set on doing this in the code behind then i think i will need a better sample or your code to help you. The following is working for me:
One Two Three
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = (sender as ComboBox).SelectedItem as String;
switch (selected)
{
case "One":
group1.Visibility = Visibility.Visible;
group2.Visibility = Visibility.Collapsed;
break;
case "Two":
group1.Visibility = Visibility.Collapsed;
group2.Visibility = Visibility.Visible;
break;
case "Three":
group1.Visibility = Visibility.Visible;
group2.Visibility = Visibility.Collapsed;
break;
default:
group1.Visibility = Visibility.Collapsed;
group2.Visibility = Visibility.Collapsed;
break;
}
}It would be worth throwing some checks in on the (sender as ComboBox) etc incase you call the event from the wrong place but i'll leave that to you to sort out
-
Groupbox visibility problem in WPF after debugging and changing the combobox valueSame sample but with three optins in the combo box working on visibility of two group boxes.
One Two Three <Setter Property="Visibility" Value="Collapsed"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Three"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers> <Setter Property="Visibility" Value="Collapsed"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers>
-
Groupbox visibility problem in WPF after debugging and changing the combobox valueTry this sample, its not working in the code behind like your example code, but insted relys on styles bindings and triggers to achieve what you are after.
One Two <Setter Property="Visibility" Value="Collapsed"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="One"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers> <Setter Property="Visibility" Value="Collapsed"/> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=Options, Path=SelectedItem}" Value="Two"> <Setter Property="Visibility" Value="Visible"/> </DataTrigger> </Style.Triggers>
-
How to write Events in Asp .Net -
Crossing the UI sync context with a datasourcethere may be a couple of options for you that i can think of straight away you can either Invoke the Add calls on the UI thread, or after everything has been added you can Invoke a mothod that triggers a refresh. I'm not very familiar with how this is done on winforms, but you can call invoke i believe on any control create on the UI thread. so something like
dgv.Invoke(new Action(() =>
{
var toAdd = dt.NewRow();
dt.Rows.Add(toAdd);
}));This may not be the neatest way but hopefully it should be a start for you. If you need a more tailored answer to your problem pop up a code sample and i'll take a look.
-
List all the open portsYou asked this question already, less than a day ago.
-
Working with WPFtry this. Note this should be added inside the following xaml node:
<Style.Triggers> <!-- INSERT FROM LINK HERE --> <Style.Triggers>