|
Wayne, firstly thank you so much for your efforts.
Seriously, I would never thought that using DataSets in WPF projects is not the best solution.
So, by now a got such code:
public DataSet GetDataSet()
{
string queryCatStr = @"SELECT * FROM [Category]";
string queryProdStr = @"SELECT * FROM [Products]";
DataSet ds = new DataSet();
SQLConnection dbConn = new SQLConnection("conn_str"))
dbConn.Open();
SQLCommand cmd = new SQLCommand(dbConn);
cmd.CommandText = queryCatStr;
SQLDataAdapter da = new SQLDataAdapter(cmd);
da.Fill(ds, "Categories");
cmd.CommandText = queryProdStr;
da.Fill(ds, "Products");
DataRelation drCat2Prod = new DataRelation("Cat2Prod", ds.Tables["categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
ds.Relations.Add(drCat2Prod);
return ds;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ds = GetDataSet();
this.DataContext = ds.Tables["Category"];
this.treeCategories.DisplayMemberPath = "CategoryName";
}
<TreeView x:Name="treeCategories" ItemsSource="{Binding}" />
...
<ListView x:Name="lstProducts" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Cat2Prod}" />
And now I just can't understand why this binding doesn't work as I expected - ListView contains products only for the first category and doesn't update contents when user selects another item in TreeView.
If I used ObservableCollection as a data source for the application, I would take care about initialization and synchronizing objects in the collection with data base. But ADO cand do it behind the scene as I think.
Even If I tried to init ObservableCollection of CategoryObjects with ProductsList contained in each one, I would make as many SQL queries to DB as many Categories I got in CategoriesTable.
Moreover, I should keep Data Base up-to-date, updating it manually. It seems to me this is not a good solution for me. But may be I'm wrong.
|
|
|
|
|
This is exactly the type of scenario that you should be looking into the Entity Framework for. Using a DataSet to bind to complex items such as a TreeView is an overly complicated task, doesn't play well with the binding notification mechanism, and actually has a poor runtime performance.
|
|
|
|
|
Thanks, Pete.
I haven't heard about it before.
I would be glad If you know some code examples similar for my task.
The main task for me is just make the code of classic ADO.NET work as expected. Possibke overhead is not a problem by now.
|
|
|
|
|
Hi,
How can i navigate to another page in a frame from code behind.
ex: I have a navigate frame, when i click on a link button from outside navigate frame i need to navigate it to another page by writing code behind.
i try ..
ContentFrame.UriMapper.MapUri(new Uri(@"/EmployeeList.xaml"));
ContentFrame.Source = new Uri("/EmployeeList.xaml");
in the click event. error i get is
Microsoft JScript runtime error: Unhandled Error in Silverlight Application Invalid URI: The format of the URI could not be determined. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString)
at HRMS.MainPage.Menu_MenuItemClicked(Object sender, EventArgs e)
at SilverlightMenu.Library.Menu.gridLevel2_MouseLeftButtonUp(Object sender, MouseButtonEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
Thankyou,
YPKI
|
|
|
|
|
I have a static method that I call from any forms code behind. The default frame is the main navigation frame but I can pass in a frame if required
public static void NavigateTo(string sViewName)
{
Frame oFR = (Application.Current.RootVisual as FrameworkElement).FindName("MainContent") as Frame;
NavigateTo(oFR,sViewName);
}
public static void NavigateTo(Frame oFR, string sViewName)
{
if(!sViewName.StartsWith("/"))
{sViewName = string.Format("/{0}",sViewName);}
oFR.Navigate(new Uri(sViewName, UriKind.Relative));
}
And call it passing in the name of the view. The view is in the app.xaml of course. Do some research into Silverlight navigation projects.
gUI.NavigateTo(sURL);
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi!
Please help me to solve the problem:
I got DataSet, which contains 2 DataTables:
Categories (CategoryID, CategoryName)
Products(ProductID, CategoryID, Name)
These tables are linked using DataRelation by CategoryID key.
So, each product has category.
I got TreeView bound to DataSet.Tables["Categories"] - no problem here. Category names are displayed correctly as tree view items(having DisplayMemberPath=CategoryName)
And I got DataGridView (imported from Windows Forms using WindowsFormsHost control).
So, I need to show Products table in this DataGridView, that contains only the products of category selected in TreeView.
I don't know how to bind DataGridView in this case. Could you please help me?
Thanks!
|
|
|
|
|
Is there any particular reason that you are using the WinForms DataGridView? WPF has got a DataGrid, which is fully compatible with data binding. You could have a property(call it SelectedCategory) in your viewmodel/codebehind which holds the selected category(bound to the SelectedItem property of the TreeView), and bind the DataGrid to SelectedCategory.Products . You then create a DataTemplate for the DataGrid which will show the particular properties of each product such as name, retail price etc.
Live for today. Plan for tomorrow. Party tonight!
|
|
|
|
|
Is there any particular reason that you are using the WinForms DataGridView? WPF has got a DataGrid, which is fully compatible with data binding.
See, I have strong restriction for my project to run under .NET version 3.5
It has no DataGrid control included. That's why I have to use DataGridView.
|
|
|
|
|
The WPF Toolkit[^] on CodePlex has a DataGrid which is compatible with .Net 3.5. If you don't like that option, the WPF ListView can be used with the View property set to Grid, and with some styling can be a very good alternative to a 'proper' DataGrid. Personally, I would use one of those options as opposed to the WinForms DataGridView.
Live for today. Plan for tomorrow. Party tonight!
|
|
|
|
|
Thanks a lot, Wayne.
Unfortunately I can't use any additional install packages for deployment. I'm just targeted on .NET 3.5 I will check the way to use ListView!
If someone has further ideas please welcome!
modified on Monday, August 29, 2011 10:10 AM
|
|
|
|
|
Unfortunately I couldn't find the way how to bind ListView to DataSet using the current selection of TreeView. Could you please provide simple example?
|
|
|
|
|
I don't really use datasets in my applications. I tend to prefer dealing with collections of CLR objects. Say I wanted to display a list of Customers in a ListView, and to have the details of the customer that is selected shown in other controls on the form for editing, I would bind my ListView to an ObservableCollection<Customer> property, and then bind the SelectedItem of the ListView to a SelectedCustomer Property in my ViewModel.
Live for today. Plan for tomorrow. Party tonight!
|
|
|
|
|
Hi,
I am a beginner in developing C# applications
I want to develop a simple application to display Google Maps through a WPF/WCF application in VS 2010.
Can anyone provide a simple tutorial on this or is it already out there?
For. example: maybe a video or written instructions?
Also, is it more easier to display Bing map in a WPF app than Google maps?
Thanks!
|
|
|
|
|
|
Image of the troubling border
Hi
I'm trying for over 2 days now to create toggle button that in pressed state looks like the above image, but that upper border is giving me a headache. Does anyone have any idea how to create that round corner that slightly goes down? The background is a linear gradient top to bottom: #b8c7d6 - #a8b3c4
Any help at all would be greatly appreciated!!
I have something like this but it is far from the design:
<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Border Background="Black" BorderThickness="1" BorderBrush="#FF4E4F50" CornerRadius="3"/>
<Border Background="Black" Margin="1" CornerRadius="3"/>
<Border Margin="2" CornerRadius="3">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#0099B9D1" Offset="0"/>
<GradientStop Color="#FF99B9D1" Offset="1"/>
<GradientStop Color="#B299B9D1" Offset="0.054"/>
</LinearGradientBrush>
</Border.Background>
</Border>
<Border Margin="2" CornerRadius="3" Opacity="0.3">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<LinearGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterY="0.5" CenterX="0.5"/>
<SkewTransform CenterY="0.5" CenterX="0.5"/>
<RotateTransform Angle="90" CenterY="0.5" CenterX="0.5"/>
<TranslateTransform/>
</TransformGroup>
</LinearGradientBrush.RelativeTransform>
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
<GradientStop Color="#00090909" Offset="0.022"/>
<GradientStop Color="#00000000" Offset="0.99"/>
<GradientStop Color="#45060606" Offset="0.001"/>
</LinearGradientBrush>
</Border.Background></Border>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
</Trigger>
<Trigger Property="IsChecked" Value="true">
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
|
|
|
|
|
Hello. After a long time i again pick WPF. Since this is nearly a hobby, i didn't learn much or forgot about it.
<Menu DockPanel.Dock="Top">
<ComboBox IsEditable="False" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Menu}}"></ComboBox>
</Menu>
I tried that. I Wanted ComboBox to use a size to take all spaced that shows menu. But in my case. It is a little to big. I do not wan't to explictly define the width of ComboBox. Can someone help me?
Thank you in advance.
Edit:
I used SizeChanged on a menu and used the folowing.
comboBoxProcessList.Width = (Double)((Menu)sender).ActualWidth - 15;
I do not like the current solution. It would be best if it was possible to do in xaml or if i could get the size of a ContentPresenter of the parrent element. In my case it is Menu
modified on Saturday, August 27, 2011 3:04 PM
|
|
|
|
|
Try using a margin or padding with the Combobox code above.
Too much of heaven can bring you underground
Heaven can always turn around
Too much of heaven, our life is all hell bound
Heaven, the kill that makes no sound
|
|
|
|
|
I did try Padding and Margin and it didn't work. I know the auto size is based on a child. But in my case i want it to be based on a parrent.
|
|
|
|
|
I've a struct called accounts (string account, int value), and a class called person with an arraylist of accounts (in C#).
I need the user to hit a button on the main window and have it bring up a listbox with SOME of the person.accounts (based on some criteria, it will vary, don't want to bury my question in code when all I need is an example), have the listbox show both the account and the value (so the whole struct, not just the account number, like "10456786 $75.50"), have the user double click on one of the accounts in the listbox and have logic execute to change the value of person.account.value for that specific account (increment up or down, I have that logic, I just don't know how to connect them).
I've been wrestling with this for a week now, digging through all the books I've managed to scavenge up (new to WPF, teaching myself), and just can't make this work. I have the pieces, but not how to connect them.
It doesn't have to be Listbox, at this point I'm open to suggestions, I'm just at my wit's end on figuring out how to do this.
Thank you in advance for your patience and consideration.
|
|
|
|
|
|
Ancient programmer, self-teaching C# and WPF, going through books but a bit lost on this one thing:
I need buttons on my main window (doing the app in WPF as much as possible) to be present/absent based upon the values of the variables in a class.
For example, I have a class person, and it has a string called branch (so person.branch). If person.branch = "" (they've not chose a branch yet), then I need the Branch button to be available. Once they select a branch (person.branch != ""), I need the button to go away (greyed out is OK, gone is better).
Just need a small code snippet, and example to point me in the right direction and I'll research the rest.
Appreciate the help in advance, sorry to bother you.
|
|
|
|
|
Hope this code will help you!!
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="209,53,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
if (branchChosen)
this.button1.Visibility = System.Windows.Visibility.Collapsed;
else
this.button1.Visibility = System.Windows.Visibility.Visible;
Best,
Vallarasu S.
|
|
|
|
|
If you are using INotifyPropertyChanged and the view / viewmodel concept, you can bind a property of the view model to the visibility.
For e.g.
Now its just setting this MyBranchProperty to the appropriate value in the view model.
Too much of heaven can bring you underground
Heaven can always turn around
Too much of heaven, our life is all hell bound
Heaven, the kill that makes no sound
|
|
|
|
|
Just a little advice from an old school programmer to another. I've learned that Binding is the only way to go with WPF. You will cause yourself more bugs, errors and headache trying to do it old school. Just step up and take the time to learn how to bind elements and such. Like I said just a friendly bit of advice.
WPF offers so much in the way of customization I sincerely hope Microsoft doesn't scrap it.
|
|
|
|
|