|
I am new to VB.net and WPF.
and i need some help.
When i select a item in the combobox.
then i will have that Directory i have selected in the combobox. add to the listbox or treeview (as a treeview with folders/subfolders/... and files)
edit:
My problem is i apparently can't use .Nodes in my VB.net code.
And my events (afterselected) is useless..
I can use it in VB.net(winforms) but not as WPF (Class Library)
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Public Class VerticalPaneExample
Dim root = "c:\temp\"
Dim matroot As String = ""
Dim newtoolsti As String = ""
Dim enodetext As String = ""
Private Sub VerticalPaneExample_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim folders() As String = IO.Directory.GetDirectories(root)
For Each folder As String In folders
Dim clean As String
clean = folder.Replace(root, "")
Me.matpicker.Items.Add(clean)
Next
End Sub
Private Sub matpicker_AfterSelect(sender As Object, e As RoutedEventArgs) Handles matpicker.AfterSelect
Me.matpicker.SelectedItem = matroot
For Each direct As String In System.IO.Directory.GetDirectories(matroot)
Dim dir As String = System.IO.Path.GetFileNameWithoutExtension(direct)
Dim newNode = viewptffiles.Nodes.Add(dir, dir)
RecurseChildFolders(direct, newNode)
Try
For Each file As String In System.IO.Directory.GetFiles(direct, "*.*")
newNode.Nodes.Add(System.IO.Path.GetFileName(file))
Next
Catch ex As Exception
End Try
Next
Private Sub RecurseChildFolders(directory As String, parent As TreeNode)
For Each direct As String In System.IO.Directory.GetDirectories(directory)
Dim dir As String = System.IO.Path.GetFileNameWithoutExtension(direct)
Dim child = parent.Nodes.Add(dir, dir)
RecurseChildFolders(direct, child)
Try
For Each file As String In System.IO.Directory.GetFiles(direct, "*.*")
child.Nodes.Add(System.IO.Path.GetFileName(file))
Next
Catch ex As Exception
End Try
Next
End Sub
End Class
My xaml file look like this:
<pre><UserControl x:Class="VerticalPaneExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ExamplePowerMillPluginVB"
mc:Ignorable="d"
d:DesignHeight="1000"
d:DesignWidth="450"
Background="White">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="380"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="10"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<ComboBox
x:Name="matpicker"
Grid.Row="1"
Grid.Column="1"
Height="20"
Width="380"
SelectedIndex="-1"
></ComboBox>
<ListBox
x:Name="viewptffiles"
Grid.Row="3"
Grid.Column="1"
Height="500"
Width="380"
/>
<Button
x:Name="Importt"
Content="Import Tool"
Grid.Row="5"
Grid.Column="1"
Height="40"
/>
</Grid>
</UserControl>
modified 1-Nov-21 6:39am.
|
|
|
|
|
You've told us what you want to do, and shown your code, but you forgot to tell us what the problem is or where you're stuck.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sorry..
My problem is i apparently can't use .Nodes in my VB.net code.
And my events (afterselected) is useless..
I can use it in VB.net(winforms) but not as WPF (Class Library)
|
|
|
|
|
|
I'm creating a WPF DataGrid. The data source is a DataTable created in the VM. See this pic.
I'm creating a DataTable in the VM, then binding the DataGrid to it. That part all works fine. I now want to apply a cell style:
View Model
string[] columnNames = (from dc in BudgetTable.Columns.Cast<DataColumn>()
select dc.ColumnName).ToArray();
foreach (string item in columnNames)
{
Binding binding = new Binding(item);
string styleName = "";
if (item == "Type")
{
binding.Mode = BindingMode.OneTime;
styleName = "budgetGridCellStyle";
}
else
{
binding.Mode = BindingMode.TwoWay;
binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
styleName = "dataGridCurrencyCellStyle";
}
var style = Application.Current.FindResource(styleName) as Style;
var col = new DataGridTextColumn()
{
Header = item,
Binding = binding,
Visibility = Visibility.Visible,
CellStyle = style
};
DataGridColumns.Add(col);
}
Style
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}">
<Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/>
<Setter Property="IsEnabled" Value="{Binding AreFieldsEnabled}"/>
</Style>
The setters won't work:
- The string format setting is not being applied no matter what I change it to. What's the right way to do this?
- The IsEnabled causes a BindingExpression error. AreFieldsEnabled is a bool property on the VM. How do I reference that from this style?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 27-Oct-21 16:36pm.
|
|
|
|
|
I need to display a modeless dialog in WPF, in an application which uses the MVVM pattern. Is there an example where this is demonstrated, starting with opening the dialog, and ending with closing this dialog.
I have tried, but that modeless dialog does not close even after the application is closed. I would like to close the modeless dialog when the application is closed.
What I have tried is this: From the ViewModel, tried to open the dialog. But this is bad MVVM practice, since the ViewModel should ideally not know anything about the views. So, my question is where exactly should I create the modeless dialog, and how to control its lifecycle? Pointers to any example Visual Studio solution workspaces are most welcome.
|
|
|
|
|
By definition, a "modeless" dialog is not a dialog; it's just a window. A dialog is a dialog when it's "modal".
And if there's a window "hanging around", it means the app is NOT "closed". In fact, you can hide all the windows, and the app is still not (automatically) closed.
You should "close" a window when you no longer need it.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I wrote an article:
WPF - Modeless Window Manager[^]
This code will automatically close any modeless windows that are open when the app is closed. Kinda handy...
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I wrote an article:
WPF - Modeless Window Manager[^]
This code will manage the modeless window for you, and provides several handy features.
In my own code, I instantiate all windows from the UI, but if it's more handy for your app to instantiate a view from the viewmodel itself, go forth and break tradition.
Remember, all of the "rules" you read are actually just guidelines. Yes, you should strive to follow industry best-practice, but you should also be flexible enough to know when you can step outside the box.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Visual Studio 2019 --- Windows Forms App (.NET Framework) C# ... Toolbox => Data => Charts provides the ability to create xy plots.
This does not exist in WPF APP (.NET Framework).
I've found multiple sites that demo a variety of shapes using .xaml and .cs. But none that demo a combo of both for an xy plot (not predefined shapes), where the .xaml defines the location and the .cs provides the data.
How do I create xy plots with WPF APP (.NET Framework)?
-- modified 18-Oct-21 11:00am.
|
|
|
|
|
Google will find you lots of WPF chart controls, both free and commercial.
I haven't used it, but this one looks good, and is both free and open-source:
GitHub - beto-rodriguez/LiveCharts2: Simple, flexible, interactive & powerful charts, maps and gauges for .Net, LiveCharts2 can now practically run everywhere WPF, WinForms, Xamarin, Avalonia, WinUI, UWP, MAUI (WIP), Blazor-wasm (WIP)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Windows Forms has an "MS Charting control"; WPF doesn't (have an "MS" charting control).
However, you can host the MS Chart control in a WPF window.
Using Microsoft Chart in WPF
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Do you have a code example? Thanks!
|
|
|
|
|
Something wrong with the link / sample?
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Sorry. I did not recognize that as a link.
When I add to .xaml
<pre>
<Grid>
<WindowsFormsHost>
<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
</WindowsFormsHost>
</Grid>
</pre>
I get this message "WindowsFormsHost is not supported in a Windows Presentation Foundation (WPF) project."
[not sure how to properly format code for this forum]
|
|
|
|
|
Ignore the chart, look up how to paint yourself in WPF. Simple hint, you use a for-loop to draw dots.
Was bloody easy in WinForms, so WPF should be easier.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
I don't believe that's what the message said because you are not trying to host a "chart" as you posted; that's a "masked text box" you're fiddling with. Focus should be your primary objective.
xmlns:dv="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
<WindowsFormsHost x:Name="uxHost"
Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch">
<dv:Chart x:Name="uxChart" />
</WindowsFormsHost>
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Solution:
Using the template on this web site - stackoverflow.com/questions/33756255/create-a-math-plot-in-c-sharp-xam
.xaml
<Canvas x:Name="gCanvasPlot0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="0,0,0,0"
Width="500"
Height="150" />
.cs
//------------------------------
private void AddPlot()
{
double dPI = Math.PI;
Polyline poXY = new Polyline {
Stroke = Brushes.Red
};
int iNumOfCycles = 3;
double dDeltaX = 0.01;
int iNumOfPoints0 = (int)(iNumOfCycles / dDeltaX);
for (int ii = 0; ii < iNumOfPoints0; ii++) {
double dX = ii * dDeltaX;
double dY = Math.Sin(2 * dPI * dX);
poXY.Points.Add(CorrespondingPoint(new Point(dX, dY), iNumOfCycles));
}
gCanvasPlot0.Children.Add(poXY);
}//AddPlot
//------------------------------
//------------------------------
private Point CorrespondingPoint(Point pt, int iNumOfCycles)
{
double dXmin = 0;
double dXmax = iNumOfCycles;
double dYmin = -1.1;
double dYmax = 1.1;
double dPlotWidth = dXmax - dXmin;
double dPlotHeight = dYmax - dYmin;
var poResult = new Point {
X = (pt.X - dXmin) * gCanvasPlot0.Width / (dPlotWidth),
Y = gCanvasPlot0.Height - (pt.Y - dYmin) * gCanvasPlot0.Height / (dPlotHeight)
};
return poResult;
}//CorrespondingPoint
//------------------------------
This works.
-- modified 18-Oct-21 11:02am.
|
|
|
|
|
Right. Now you can start thinking about axis labels, legends, titles, data sources, etc.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
(shrug, should have posted this here before, newbie error)
I have a UI with a scrollviewer like this:
I want to scroll the viewer to the top when the ItemSource changes.
<ScrollViewer x:Name="Scroller" Grid.Row="2" Margin="0,0">
<StackPanel>
<ItemsControl x:Name="containers"
ItemsSource="{Binding Whatever, NotifyOnTargetUpdated=True}"
TargetUpdated="Containers_OnTargetUpdated">
</ItemsControl>
<StackPanel>
<ScrollViewer>
public partial class MyPage: UserControl
{
public MyPage()
{
InitializeComponent();
}
private void Containers_OnTargetUpdated(object? Sender, DataTransferEventArgs E)
{
var MyScroller = (ScrollViewer) this.FindName("Scroller");
MyScroller.ScrollToHome();
}
}
This works but it's probably not the best way to do it (ie. finding a control by name).
Can I pass the ScrollViewer "object" to the TargetUpdated callback/event ? so that I don't have to referencing it by name?
It seems clunky.
I don't know exactly what magic incantations I need to google for.
Thanks. M.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Already answered in the C# forum:
Re: (xaml, wpf) ScrollViewer Binding confusion (beginner) - C# Discussion Boards[^]
private void Containers_OnTargetUpdated(object? sender, DataTransferEventArgs e)
{
var current = sender as DependencyObject;
var scroller = current as ScrollViewer;
while (scroller == null && current != null)
{
current = VisualTreeHelper.GetParent(current);
scroller = current as ScrollViewer;
}
if (scroller != null)
{
scroller.ScrollToHome();
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I don't understand your apprehension about the "direct" approach ... or is this another "MVVM" thing?
private void Containers_OnTargetUpdated(object? Sender, DataTransferEventArgs E) {
this.Scroller.ScrollToHome();
}
The compiler recognize the name you gave to the ScrollViewer in the XAML (for that page / control); you don't need "reflection" in this case.
For other "public" cases, add a public "getter" for the control in question.
I assume you have some idea when the ItemsSource to the list is changed; set the scroll then if event handling is too confusing.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I'm working on a piece of UI where the user enters a dollar amount. The field is bound to a decimal.
Whenever I press the decimal point, here's a pic of what I get
Here's the xaml:
<TextBox Grid.Column="5"
Text="{Binding Entity.InvoiceAmount, StringFormat=C, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Margin="2"
Width="200"
Style="{StaticResource textBoxStyle}"
TextAlignment="Right"
IsEnabled="{Binding AreFieldsEnabled}"/>
The only way I can seem to change the decimal portion is to arrow over to it.
What's wrong here?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
It's the same problem you had entering a percentage back in April:
Re: Format Percentage - WPF Discussion Boards[^]
Change UpdateSourceTrigger to LostFocus instead of PropertyChanged .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
How did you remember that?? I totally forgot I posted that.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|