Whats wrong with my code? Any reference missing? - c#

I have been trying to add the navigation tool that will simply zoom in and zoom out. Push left and right using the click of the mouse.
I'm using in the Silverlight, Visual Studio 2010.
The XAML file contains the following:
<esri:Navigation Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom"
Map="{Binding ElementName=MyMap}">
</esri:Navigation>
The C# file contains the following:
private void MyMap(object sender, RoutedEventArgs e)
{
MyMap.Focus();
}
I have added the reference dll file : ESRI.ArcGIS.Client.Toolkit
Though the navigator is visible on the map. But unable to function. Please assist the same.

I'm not sure what your MyMap(object, RoutedEventArgs) method is supposed to do, but MyMap needs to be the name of an esri:Map in your XAML file, not a method name. This blog post has a good simple example near the start.

Related

Using two different events to update a ViewModel property in WPF

I apologize if the question title isn't really specific, I'm not exactly sure how to condense the problem I'm having down to a few words. But to simplifiy the problem I'm having, here is my issue:
I'm creating a tool using WPF that consists of a TextBox that will contain a path to a directory and a Button that will allow you to Browse to a certain directory. Now, when I select the Browse button, it pops up a dialog, allows the user to select a directory and then I have some methods that will disable some buttons and updates some Brushes on the screen if the path doesn't meet a certain set of criteria. No problems there, got that working.
My problem is the TextBox that this Browse button correlates with. This TextBox is using a binding as such:
In my MainWindow.xaml (Yes, this is the simplified, focused version):
<Window>
<TextBox Text="{Binding Directory}" TextChanged="Directory_TextChanged" />
<Button Content="Browse..." Click="Browse_Click"/>
</Window>
In my code MainWindow.xaml.cs file:
public partial class MainWindow: Window
{
private ViewModel myViewModel;
public MainWindow()
{
myViewModel = new ViewModel();
this.DataContext = myViewModel;
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
// Dialog stuff that's working
viewModel.Directory = dialog.SelectedPath;
}
private void InstallDir_TextChanged(object sender, TextChangedEventArgs e)
{
ValidatePath(); /* Disables/enables buttons and updates brushes based on validation. Also working */
}
private void ValidatePath() {/* */}
}
Like I mentioned earlier, the browse button works fine. I'm trying to figure out however, how I can get this to work if I type a directory alongside it. Because if I type something in the textbox, that would mean that inside of the InstallDir_TextChanged() function I would have to set viewModel.Directory, but since I have the INotifyPropertyChanged attached to this ViewModel, this function would get called recursively.
I tried doing the validation stuff within the viewmodel, but I couldn't figure out how to update the brushes/buttons in MainWindow if I did this. (Still relatively new to C# so I haven't learned the ins and outs yet. This is the first WPF tool I've been making from scratch, so just a disclaimer).
Would anyone have any ideas (or logic) I can approach to try and accomplish this? If there's any further clarification needed, that's not an issue. I don't need an exact definitive answer. Maybe some advice that could point me in the correct direction would definitely suffice. I don't have a problem trying to figure stuff out.

C# / WPF - Getting object inside a child user control from parent window

Not terribly familiar with WPF and C# so if this is blatantly wrong, please correct me. Working in VSExpress2015 .NET Framework 4.5. I'm heavily simplifying my code below, so know that namespace/library references are there.
Say I have a window with a Button and ContentControl inside:
<Window x:Class="Project.MainWindow">
<Grid>
<Button Name="Submit_Btn" Click="Submit_Btn_Click">
<ContentControl Name="MainContentControl">
</Grid>
</Window>
I also have several user control files in my project with XAML that looks something like this:
<UserControl x:Class="Project.UserControl1">
<Grid>
<TextBox Name="TxtBox1">
</Grid>
</Usercontrol>
I have code in the backend of my MainWindow to dynamically load the appropriate UserControl into the "MainContentControl" object. However, I want to reference the objects inside of the currently loaded UserControl from the MainWindow's Submit_Btn_Click function. For example, in MainWindow.cs, do something like this:
private void Submit_Btn_Click(object sender, RoutedEventArgs e)
{
if(MainContentControl is currently loaded with UserControl1)
Do_Something_Function(MainContentControl.TxtBox1.Text);
}
The main problem here is I don't know how to call the TextBox1 element from within the parent MainWindow's scope. I'm also not sure how to validate the if condition (confirming the control is currently loaded). Does anyone know of a way to think about this differently or even directly reference the object (despite that probably not being a great idea)?
--
I'm not using/familiar with MVVM at all (yet), and I'm not particularly concerned with optimal performance as this is a one off temporary project that will soon die and be re-worked. I've read ways how to access the data in a parent window from a child, but I didn't find scenarios that really matched up with this.
Once again, I'm still familiarizing myself with C#, WPF and general coding practices (it's been a couple years), so if using a ContentControl or UserControl here isn't optimal, (or mixing the two doesn't make sense) that information would be greatly appreciated; however, in this scenario, I'm more concerned with just getting this working until I can learn more proper techniques later.
Instead of trying to access the TextBox inside the UserControl, you can expose properties and methods on the UserControl itself to interact with what's inside. In your case, you could add a property that returns the current value of TextBox.Text. You can also add dependency properties to facilitate binding.
You can use the LogicalTreeHelper to search by name.
So for your example to access TextBox1
var txtBox = LogicalTreeHelper.FindLogicalNode(MainContentControl, "TextBox1") as TextBox;
Do_Something_Function(txtBox?.Text);
It definitely looks like the design should be improved, but just to get this working you can do the following:
private void Submit_Btn_Click(object sender, RoutedEventArgs e)
{
var controlAsUserControl1 = MainContentControl.Content as UserControl1;
if (controlAsUserControl1 != null)
{
Debug.WriteLine(controlAsUserControl1.TxtBox1.Text);
}
}

Pausing/Stopping Gif animation in WPF

I am using gif animation in WPF application, an external dll WpfAnimatedGif.dll is used for showing the animation. The animation is working properly, but I am unable to pause/stop it on a certain event let say button click, how can I do it, kindly guide me, my xaml code is given below
<Image x:Name="scaner" Stretch="UniformToFill" gif:ImageBehavior.AnimatedSource="{StaticResource gifImage}" >
According to your documentation (which you should have read yourself) this will work:
private void Button_Click(object sender, RoutedEventArgs e)
{
var controller = ImageBehavior.GetAnimationController(this.scaner);
controller.Pause();
}
Please note that working directly with button functions is not the prefered way in WPF, if you want to use the full power I suggest using MVVM and the command pattern instead.
You can try this, use reference
xamlAnimationWpf Nuget <br>
var controller = AnimationBehavior.GetAnimator(myGif); <br>
controller.Pause();<br>
For those who try another way

Modify Resource in Design Time, with code behind in WPF project

What I'm trying to do right now: Modify the Expression Blend UI / Visual Studio, to add a button on one of my dependency properties, and when I click on it, it creates a new trigger.
What is working: I created the button, it appears in the UI, that's fine.
What is not working: I cannot modify the Resource to add a trigger (if I step in, it works but it does not modify on the global resource, only on the instance I think).
I have my main project in Visual Studio, and a property with a button like this:
When the button is pressed this is what happens:
I get my Control that contains that dependency property (Ok).
I searched for the Resource file that contains the Resource I want to modify (Ok).
I update the Resource, but it does not replace the Resource on disk.
I think that it's because I only modify it on memory, so it's in the "air"
I don't know where to go now... I need help
The code behind where I modify the Resource is in an other DLL, the MyLibrary.Design.cs
I'm using Visual Studio 2010 / Blend 4 / .NET 4.0
That's one splution I can figure at the moment, but unfortunately I'm not able to test it now. Still you can check if it works for you.
You can have a class, say ResourceContainer.cs, and collect your control in it as a public static value, and let your control be a button:
public static Button MyButton;
Then you can use it in your code-behind:
If your window is MainWindow.xaml, and, say you need your particular control in a grid, then you probably have something like:
<Window x:Class=...
...(namespace stuff)...
Title="MainWindow" ...(size stuff)...>
<Grid x:Name="MyGrid">
...(your code here)...
</Grid>
...
</Window>
Then in MainWindow.xaml.cs you can use ResourceContainer.cs like this:
...
ResourceContainer rc;
...
MyGrid.Resources.Add("MyKey", rc.MyButton);
...
(when you need it)
rc.MyButton.Triggers.Add(TriggerBase item);
...

How to open a window then load my XAML file to it? (XAML and C#)

I am new to WPF and C# and i need some help - sorry about the newbie question!
Anyway I have a control panel 'window' as the file thats loaded when i run my project, and i then have a button on this control panel, that when clicked triggers an event. Inside this 'event function' I am trying to load a new window that has its own XAML code behind, how do i go about doing this? I have googled but to no avail.
Please explain in laymens terms, I am still getting the hang of this all.
Thanks!
private void btnCustomers_clicked(object sender, RoutedEventArgs e)
{
//load in Customers.xaml file here - in a new window
}
You need to declare an instance of the class that is your other window then call Show() on it. So if your other window is call MySecondWindow you write the following in your event handler.
MySecondWindow otherWindow = new MySecondWindow();
otherWindow.Show();
A basic explination of how windows work can be found on the MSDN Site.

Categories

Resources