|
Hi,
I am developing a WPF application in MVVM pattern.
I got stuck in closing a popup window. I will explain how I started my application.
I created Main window, Login User Control and New User window.
In my main window , I used content control to load my login user control. On clicking the new user button in login screen, I initiated new user window and then I filled user registration details in new user window and then clicked OK. Now my new user window must get closed and stay with main window but it is not happening.
But I am not able to close the new user window through my button click.
Kindly help me in this situation and provide me a best approach to solve the issue.
Thanks in advance
Regards,
Selvam S
|
|
|
|
|
Personally I use code behind to handle the click event of the button and set the DialogResult. Any processing of the data is done in the VM and is called from the code behind.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
This[^] is a fairly common solution to this problem.
|
|
|
|
|
If you look at the Prism samples[^], you will find a good solution that you could use to implement a self-closing popup dialog.
Just handle the mouse click event on the shell and you can close any popups that are open on the shell.
|
|
|
|
|
When I looked at WPF and MVVM I solved this by creating a base UserControl that handled showing itself on a form - either modally or not.
The ViewModel then has the ability of telling the view to close itself
Personally I think it is a far simpler solution than any others I have come across
You can see it in action if you read my articles (link in signature)
|
|
|
|
|
Hi there.
I have a control, that depending on its size and diferent parameters return me a value. Issue is that I need to do this in xalm and not in code, and do not know how to pass a tuple using xaml.
Now I have it by code:
Binding dataBinding = new Binding();
dataBinding.Source = this.ZoomMemoryArea.MemoryAreaData;
dataBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
dataBinding.Path = new PropertyPath("Size");
dataBinding.Converter = new BlockingRectangleMemorySizeConverter();
dataBinding.ConverterParameter = new Tuple<Canvas, IMemoryAreaData>(this.zoomCanvas, this.MemoryArea.MemoryAreaData);
dataBinding.Mode = BindingMode.OneWayToSource;
this.zoomControl.SetBinding(Control.WidthProperty, dataBinding);
And the ???? is what I really do not know how to do.
<pre lang="xml"><ContentControl.Width>
<Binding Mode="OneWayToSource" UpdateSourceTrigger="PropertyChanged" Converter="{StaticResource memorySizeConverter} ConverterParameter=???? Path="MemoryAreaData.Size" Source="ZoomMemoryArea">
</Binding></pre>
As for multiBinding, I can't use it as this is a OneWayToSource binding so I would not get more than one value.
Any sugestion would be much apreciated.
Regards
Forgot to add the converter
|
|
|
|
|
I have an XML file structured this way:
<Emulators>
<System>
<Name>Name1</Name>
<Kind name="lexplode"/>
<Kind name="sexplode"/>
<Kind name="dropbomb"/>
</System>
</Emulators>
If I want to read, in XAML, from <Name> I know that I have to do:
<TextBox Text="{Binding XPath=Name, UpdateSourceTrigger=PropertyChanged}"/>
But what if I want to read all the values under <Kind>> ?
And which control could receive them, a textbox, a listbox, ..?
Thanks a lot!!
modified 7-Sep-12 2:43am.
|
|
|
|
|
In order to get the attribute, you have to use a slightly different type of syntax. Rather than using XPath=name , you have to use XPath=@name . Now, that will only get you the value of the currently selected Kind item, so how do you actually get that item? Well, the answer is to use something like a ListBox to encapsulate the items.
<ListBox ItemsSource="{Binding Source={MyEmulatorsList, XPath=//Kind}" ItemTemplate="{StaticResource KindItemsTemplate}" /> Note: In this sample, I'm assuming that you have used an XmlDataProvider called MyEmulatorsList to point to this XML. So, where is the actual display of the name item? Well, that's provided by the ItemTemplate, which looks like this:
<DataTemplate x:Key="KindItemsTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
|
|
|
|
|
Thanks for your reply!!
I think that I'd better expose the whole code.
XML file:
<Emulators>
<System>
<Gear>Name1</Gear>
<Kind name="att1"/>
<Kind name="att2"/>
<Kind name="att3"/>
</System>
</Emulators>
VB code:
Class MainWindow
Dim data As XmlDataProvider = New XmlDataProvider()
Private Sub MainWindow_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
data.Source = New Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\myapplication\Systems.xml")
data.XPath = "Emulators/System"
Grid1.DataContext = data
End Sub
End Class
XAML code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525" Background="#FF4E4E64">
<Grid Name="Grid1">
<Grid.Resources>
<DataTemplate x:Key="KindTemplate">
<TextBlock Text="{Binding XPath=@name}" />
</DataTemplate>
</Grid.Resources>
<TextBox Text="{Binding XPath=Gear, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="197,230,0,0" Name="TextBox3" VerticalAlignment="Top" Width="220" />
<ListBox ItemsSource="{Binding Source=data, XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}" Margin="261,51,12,149" />
</Grid>
</Window>
It doesn't work, I get a
System.Windows.Data Error: 44 : BindingExpression with XPath cannot bind to non-XML object.; XPath='//Kind' BindingExpression:Path=; DataItem='String' (HashCode=2037252866); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable') data
modified 7-Sep-12 13:08pm.
|
|
|
|
|
You are trying to bind to Emulators in your XPath, but your XML says Base.
|
|
|
|
|
You are trying to bind to Emulators in your XPath, but your XML says Base.
That's a typing mistake in my post (I've corrected it in my post)...but actually the XML file is correct..so the mistake doesn't come from here.
|
|
|
|
|
I think I see what the problem is. In the ListBox XAML, you don't need the Source=data (that's incorrect syntax, and besides, you already set the DataContext from the code-behind, so it's not needed. Take that part out, and it should work:
<ListBox ItemsSource="{Binding XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}"
Margin="261,51,12,149" />
|
|
|
|
|
Thank you very much!! For the first time I can see those data displayed!!
Just one think... of importance:
ALL the "Kind" attributes are displayed.
I've tried a "IsSynchronizedWithCurrentItem="True"" but it doesn't change anything.
Any idea on how to fix it, please?
The datatemplate for the combobox that displays the system's "Name":
<DataTemplate x:Key="SystemTemplate">
<Label Content="{Binding XPath=Gear}"/>
</DataTemplate>
The datatemplate for the ListBox that display the "Kind" name attributes (related to the selected "System"!!):
<DataTemplate x:Key="KindTemplate">
<TextBlock Text="{Binding XPath=@name}"/>
</DataTemplate>
The WPF controls:
<ComboBox Height="37" HorizontalAlignment="Left" Margin="12,262,0,0" Name="ComboBox1" VerticalAlignment="Top" Width="167"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource SystemTemplate}"
IsSynchronizedWithCurrentItem="True">
</ComboBox>
<ListBox ItemsSource="{Binding XPath=//Kind}" ItemTemplate="{StaticResource KindTemplate}" IsSynchronizedWithCurrentItem="True" Margin="425,35,12,149" />
modified 8-Sep-12 6:48am.
|
|
|
|
|
You can select an item by using the XPath expression. Instead of XPath=//Kind, use XPath=//Kind[0], or XPath=//Kind[1], etc.
|
|
|
|
|
dbaseman,
Thanks again for taking time to help me!
If you consider my XML file
<Emulators>
<System>
<Gear>Name1</Gear>
<Kind name="a1"/>
<Kind name="a2"/>
<Kind name="a3"/>
</System>
<System>
<Gear>Name2</Gear>
<Kind name="b1"/>
<Kind name="b2"/>
<Kind name="b3"/>
</System>
</Emulators>
Using "XPath=//Kind[1]" will return: "a1, b1"
What I would like is "a1, a2, a3" being returned!!
How should I please proceed to achieve this (returning all "Kind" attributes of the currently selected "System"?
Thanks very much,
modified 9-Sep-12 6:20am.
|
|
|
|
|
I'm not really an XPath expert, but my guess would be: //System[1]/Kind
|
|
|
|
|
I 'd like to creatre a gallery control just like the built-in gallery in Windows Phone 7. I put a listbox in the grid to show the thumail image, but it cost many memory. And crashed soon after I loaded more than 80 pics(640*480). Could anyone give me a help?
|
|
|
|
|
Thanks for the reply!
I had added the item items to the listbox using databiding like this:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="2" Tag="{Binding ThumID}" Tap="Grid_Tap">
<Image x:Name="selTag" Width="32" Height="32" Source ="/Resources/Images/sel.png" Visibility="Collapsed" Tag="{Binding FilePath}" VerticalAlignment="Bottom" HorizontalAlignment="Right" Canvas.ZIndex="5"/>
<Image Width="160" Height="120" x:Name="thumImage" Stretch="Uniform" CacheMode="BitmapCache" Source="{Binding ThumBitMap}" />
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Width="200"orderThickness="5" Opened="contentMenu_Opened" Closed="contentMenu_Closed" IsZoomEnabled="False" Background="#c8b3d9ff" BorderBrush="LightGray">
<toolkit:MenuItem x:Name="cm_Del" Header="Delete" Click="cm_Del_Click" Tag="{Binding}"/>
<toolkit:MenuItem x:Name="cm_Edit" Header="Edit" Click="cm_Edit_Click" Tag="{Binding}"/>
<toolkit:MenuItem x:Name="cm_Share" Header="Share" Click="cm_Share_Click" Tag="{Binding}"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
|
|
|
|
|
Hello
I have a TreeView with a HirarchicalDataTemplate. Within this template I would like to set the background of the TreeViewItem depending on a property behind the template.
Thats my xaml:
<TreeView.Resources>
<con:TelegramGroupStateToColorConverter x:Key="TelegramGroupStateToColor"/>
<HierarchicalDataTemplate
DataType="{x:Type tree:ModelTelegramGroup}"
ItemsSource="{Binding Children}"
>
<TextBlock
Text="{Binding Path=Group.Name}"
Tag="{Binding}"
p:ExContextMenu.Menu="{Binding ContextMenu}"
Background ="{Binding Path=Group.State, Converter={StaticResource TelegramGroupStateToColor}}"
/>
</HierarchicalDataTemplate>
</TreeView.Resources>
In opinion it is not rocket sience but it does not work. I can set a breakpoint within the Converter(TelegramGroupStateToColor) and it will be hit and the correct color will be returned, but the backround remains transperent.
Has anybody an idea what I am doing wrong?
|
|
|
|
|
You haven't said what your converter returns. Make sure it returns a System.Windows.Media.Brush and not just a Color or a System.Drawing.Brush. Otherwise this should work.
Hope this helps
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Thanks a lot for the quick answer. You were right.
I returned a System.Drawing.Brush I changed it now and it is working.
Thanks
|
|
|
|
|
Glad to help
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
It is polite to reward a correct answer with n upvote!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
I have found a confusing behavior in ContextMenu when processing bound Commands. I want to bind/define a CommandParameter by ItemContainerStyle:
<ContextMenu>
<MenuItem Command="{Binding MyCommand}" />
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="Item" />
<Setter Property="CommandParameter" Value="ValidParameter" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
When the menu opens, the CanExecute is called with a command parameter == null! However it is called with the expected parameter immediately before executing the command. But as I want to have the CanExecute return value to depend on the command parameter (enable or disable the menu item), I need the correct parameter also when opening the menu.
Following is a test application to show the bad behavior (see program output for the passed parameter). I would be happy if somebody could help me. By the way, when I directly bind the parameter in MenuItem, everything works fine, but I don't want this because I want to dynamically build the menu using MVVM later.
Thanks!
XAML:
<Window x:Class="CommandParameterExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<Border Margin="4" Height="50" Background="Green">
<Border.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding MyCommand}" />
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="Item" />
<Setter Property="CommandParameter" Value="ValidParameter" />
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Border.ContextMenu>
</Border>
</StackPanel>
</Window>
Code-Behind:
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
namespace CommandParameterExample
{
public partial class MainWindow
{
public ICommand MyCommand { get; private set; }
public MainWindow()
{
MyCommand = new CommandModel();
InitializeComponent();
}
}
internal class CommandModel : ICommand
{
public bool CanExecute(object parameter)
{
WriteParameter(parameter, "CanExecute");
return true;
}
public void Execute(object parameter)
{
WriteParameter(parameter, "Execute");
}
private static void WriteParameter(object parameter, string context)
{
Debug.WriteLine("Parameter in {0}: {1}", context, parameter ?? "null");
}
public event EventHandler CanExecuteChanged;
}
}
modified 4-Sep-12 5:07am.
|
|
|
|
|
Hello,
I have a ListBox which is bound to an ObservableCollection, using some (complex) ItemTemplate to display the data. It is guaranteed that there is a fixed number of items within the ItemsSource.
Every time, the ObservableCollection changes (e.g., an item is replaced), a new ItemTemplate is generated. Can I somehow avoid this and instead reuse the existing template object and simply bind this to the new data?
My idea was to use a dynamic resource:
<Page.Resources>
<my:Chart x:Key="chart"/>
</Page.Resources>
...
<DynamicResource ResourceKey="chart"/>
But how can I, in this case, set the Properties of the chart (e.g. the DataContext, or the Grid.Row as the object is embedded within a Grid)?
A second problem is: If I have n elements within the ObservableCollection, I need n chart objects. How can I make sure to use a different chart object for each observable?
Alex
|
|
|
|