public Window1()
{
InitializeComponent();
base.DataContext = new DataSource();
var items = new string[] { "A", "B", "C" };
this.tab.ItemsSource = items;
var collView = CollectionViewSource.GetDefaultView(items);
collView.CurrentChanging += this.OnTabItemSelecting;
}
void OnTabItemSelecting(object sender, CurrentChangingEventArgs e)
{
bool allow = this.chk.IsChecked.GetValueOrDefault();
if (!allow)
{
// SelectedContent hasn't changed yet, so use it to figure out
// the index of the previously selected tab
int prevIdx = this.tab.Items.IndexOf(this.tab.SelectedContent);
this.tab.SelectedIndex = prevIdx;
}
}
Public Sub Window1()
InitializeComponent()
MyBase.DataContext = New DataSource()
Dim items = New String() {"A", "B", "C"}
Me.tab.ItemsSource = items
Dim collView = CollectionViewSource.GetDefaultView(items)
collView.CurrentChanging += AddressOf Me.OnTabItemSelecting
End Sub
Private Sub OnTabItemSelecting(ByVal sender As Object, ByVal e As CurrentChangingEventArgs)
Dim allow As Boolean = Me.chk.IsChecked.GetValueOrDefault()
If Not allow Then
Dim prevIdx As Integer = Me.tab.Items.IndexOf(Me.tab.SelectedContent)
Me.tab.SelectedIndex = prevIdx
End If
End Sub
But I have no idea how that vb.net code works.
So, please somebody explain me how that vb.net code works in WPF.
Last edited by Kram Kramer; Feb 26th, 2018 at 02:19 AM.
I'm not seeing where you've provided any information about those issues. If you want use to help you with conversion issues then you should be providing the original code (as formatted text in your post, not an off-site image) and the converted code, along with specific details of what "doesn't work" actually means so that we have an idea what to look for. That's how you help us help you.
Alternatively, download Instant VB from Tangible Software Solutions. There's a free version and it will likely produce an error-free result.
For one thing, the first method is a constructor, so the code it contains should be put into the New sub in your VB window, because that's the constructor. Also, does your window contain a TabControl named 'tab'?
Class MainWindow
Public Sub Window1()
InitializeComponent()
MyBase.DataContext = New DataSource()
Dim items = New String() {"A", "B", "C"}
Me.tab.ItemsSource = items
Dim collView = CollectionViewSource.GetDefaultView(items)
collView.CurrentChanging += AddressOf Me.OnTabItemSelecting
End Sub
Private Sub OnTabItemSelecting(ByVal sender As Object, ByVal e As CurrentChangingEventArgs)
Dim allow As Boolean = Me.chk.IsChecked.GetValueOrDefault()
If Not allow Then
Dim prevIdx As Integer = Me.tab.Items.IndexOf(Me.tab.SelectedContent)
Me.tab.SelectedIndex = prevIdx
End If
End Sub
End Class
So, you're just ignoring the bit about the constructor? Create an appropriate window, i.e. a window with the appropriate control(s) and then add a constructor to the code. Next, copy the code from the first method (which is a constructor in the C# code) and then copy the second method (the event handler) as is. If you still have issues, please describe them clearly. Don't post screenshots. Post the code, highlight the lines that are at issue and describe the issues, which means providing error messages if there are any.
If all you're going to do is copy code from elsewhere then that's your prerogative but that's not programming, so I'm not much interested in helping you do that. If you want to learn how to program then you actually need to learn the basics first and then apply them. People should be able to use terms like class, method, property, constructor, event, etc and you should know what they mean or know how to find out. We shouldn't have to teach the basics each time we answer a specific question. As I said, what you do is up to you but if you want to jump in without a decent grounding in the fundamentals then you're going to get stuck on easy stuff and I don't see it as my job to help people with the easy stuff that they can find for themselves without much effort. I want to help with the hard stuff that people have attempted but can't get on their own. How to write a constructor doesn't fall into that category as far as I'm concerned. Others may feel differently and you're welcome to wait and see if someone else does, but It would be a good idea to research the fundamentals in the mean time.
Based from the screenshot provided, you need to import the System.ComponentModel namespace to use the CurrentChangingEventArgs class and make sure to add a checkbox control with name chk in your XAML since it's looking for that control which does not exist in your XAML.