Wpf send layout object not by button or textbox - c#

I've got a little bit problem with sending my layout object. I'm trying to create an WPF app in style like a chrome with draggable cards. To do this, I'm using OpenControls.Wpf library + Prism + MahApps.Metro
Layout declarated in main xaml view
<dockManager:LayoutManager x:Name="_layoutManager" Grid.Row="1" Grid.Column="0"
DocumentsSource="{Binding Documents}" ToolsSource="{Binding Tools}"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" UseLayoutRounding="False"
Margin="0,-4,0,0">
<dockManager:LayoutManager.Theme>
<themes:ModernTheme/>
</dockManager:LayoutManager.Theme>
<dockManager:LayoutManager.DocumentTemplates>
<DataTemplate DataType="{x:Type usViewModel:UsersModule_ViewModel}" >
<usViews:UsersModule_View x:Name="UsersModuleModule" Margin="4"/>
</DataTemplate>
</dockManager:LayoutManager.DocumentTemplates>
</dockManager:LayoutManager>
Buttons to open projects in card in the same xaml file
<StackPanel>
<Button x:Name="myButton" Style="{StaticResource ButtonMore}" Content="Users"
Command="{Binding LoadModule1Command}"
CommandParameter="{Binding ElementName=_layoutManager}"
HorizontalAlignment="Left" Margin="24,80,24,0" VerticalAlignment="Top" Width="208" Height="40"/>
</StackPanel>
By the DelegateCommand { get; private set; } in my module, through the button CommandParameter is sending an layoutManager object. I need to get that object, but without click any button or textbox.
So my main question:
Is it possible, to send an object without any buttons or is it possible to programmatically call of the click event in WPF.
I've search something about that problem, but did not find any solution.

Related

WPF Button click has no references

I have a button inside a Grid container that is inside a ScrollViewer Container.
When I double click the button to auto fill the button click code in c# I get no references.
<ScrollViewer x:Name="_scroll">
<Grid Height="584">
<Button x:Name="n_one" Content="N" HorizontalAlignment="Left" Margin="134,165,0,0" VerticalAlignment="Top" Width="30" FontSize="18" FontWeight="Bold" Height="23"/>
</Grid>
</ScrollViewer>
There is more stuff in the Grid Container but they are just labels and buttons. They other elements in the container aren't suffering from the same issue.
private void n_one_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Working")
}
I do have the Console setup and working shown with previous code. Visual Studio is telling me i have 0 references to n_one_Click why is this?
If this changes anything, I did copy this button a few times, I did rename these buttons, but they also have the same problem, elements that aren't related but still in the same container aren't affected.
If you really want the n_one button to fire your n_one_Click method, I would expect your XAML to look like this:
<ScrollViewer x:Name="_scroll">
<Grid Height="584">
<Button
x:Name="n_one"
Content="N"
Click="n_one_Click" <========== Here
HorizontalAlignment="Left"
Margin="134,165,0,0"
VerticalAlignment="Top"
Width="30"
FontSize="18"
FontWeight="Bold"
Height="23"/>
</Grid>
</ScrollViewer>

How to catch activated command from child component to parent component in WPF?

I've got a MainWindow that contains a main menu. The menu is a UserControl, consisting of a StackPanel with Buttons inside.
The simplified XAML code is as follows:
<Window>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<local:MainMenu Grid.Column="0"></local:MainMenu> <!-- the navigation menu -->
<ContentControl Grid.Column="1" Content="{Binding CurrentView}" />
</Grid>
</Window>
<!-- The navigation menu UserControl -->
<UserControl Class="MyApp.MainMenu" ...etc... >
<StackPanel>
<Button Style="{StaticResource menuButton}">To do</Button>
<Button Style="{StaticResource menuButton}">Notes</Button>
<Button Style="{StaticResource menuButton}">Settings</Button>
<Button Style="{StaticResource menuButton}">News</Button>
<Button Style="{StaticResource menuButton}">Calendar</Button>
<Button Style="{StaticResource menuButton}">...</Button>
<Button Style="{StaticResource menuButton}">...</Button>
<Button Style="{StaticResource menuButton}">...</Button>
</StackPanel>
</UserControl>
Now of course, when a Button in the menu UserControl is clicked, I want to notify the MainWindow that the user wants to navigate. The MainWindow has to change its CurrentView property. To show the corresponding view.
The problem I have is that I can't get any communication between components. It seems very hard to achieve this in WPF. There are tutorials on the internet, but they are not explaining well enough how to do it the right way.
I think it's a bad idea to just add Click event handlers to each button of the MainMenu. I would like to solve this using commands, and use a command parameter to determine what Button was clicked if that's good practise.
In the most ideal situation, could it be made that the menu just 'emits' something like an event, and that the window catches this? This way the child control (the menu UserControl) wouldn't have to be aware of its parent.
Can someone help me?
There are many ways you can do it. If you are using MVVM, you can just do command binding, if you have lets say ExitApp command in your MainWindow viewmodel,then you can do like this:
<vw:ImgButton Name="Exit" ClickCommand="{Binding DataContext.ExitApp, RelativeSource={RelativeSource AncestorType={x:Type Window}},Mode=OneWay}"/>
Optionally you can provide info in command parameter and do switching inside ExitApp command as well.
Made a mistake and review my answer. ContextMenu are not in the same "context" and binding are not working easily - I use a Forward command concept so menutiem command are forwarded to the MainViewModel - tell me if you need more, see in my project CBR and search for ForwardCommand

Binding Button to Listbox Item WPF

I have a GUI containing a listbox that holds rows of missing third party updates that resembles the following
INSTALLBUTTON | POSTPONEBUTTON | Application Name ApplicationVersion Upgrade Message
INSTALLBUTTON | POSTPONEBUTTON | Application Name ApplicationVersion Upgrade Message
Each row contains two buttons (Install and Postpone) as well as several properties read in from a list (application name, version, number of used deferrals, message that is displayed in red). The datacontext for the listbox is a list.
I'm having trouble figuring out how to bind the Install and Postpone buttons to the applications/rows. This is my current WPF:
<ListBox Name="ThirdPartyListBox" ItemsSource="{Binding}" Margin="0,70,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="C:\Users\test\Desktop\Project\ACME-WPF\ACME-WPF\window-new-3.ico" Margin="5" Width="50"/>
<Button Name="ThirdPartyInstallButton" Content="Install" Click="InstallThirdPartyUpdatesButton_Click" Margin="5,5,0,0" Height="25"></Button>
<Button Name="ThirdPartyPostoneButton" Content="Postpone" Margin="5,5,0,0" Height="25"></Button>
<TextBlock FontWeight="Bold" Text="{Binding Item2.Name}" Margin="12,25,0,0"/>
<TextBlock FontWeight="Bold" Text="{Binding Item2.RequiredVersion}" Margin="3,25,0,0"/>
<TextBlock Text="{Binding Item2.CustomUIMessage}" Margin="10,25,0,0" TextWrapping="Wrap" Foreground="Red"/>
<TextBlock Text="You have used " Margin="3,25,0,0"/>
<TextBlock Text="{Binding Item3.UsedDeferrals}" Margin="3,25,0,0"/>
<TextBlock Text=" of " Margin="3,25,0,0"/>
<TextBlock Text="{Binding Item2.MaxDefferals}" Margin="3,25,0,0"/>
<TextBlock Text=" deferrals for this update." Margin="3,25,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Once the Install button is clicked I need to execute a method that will trigger an install, using a number of the properties of the associated application list item.
In the click handler of the button, call the button .DataContext, that should have your object and its properties.
Instead of using "Click" use the command structures. To get the associated item's data in the command, use the CommandParameter.
CommandParameter="{Binding Path=.}" Command="{Binding ElementName=Root, Path=DataContext.InstallComponentCommand}"
This assumes that the Window/UserControl's is called "Root". Then, in your code-behind set up a "DelegateCommand" to do the work. This blog post contains a great tutorial on how to set it up: http://wpftutorial.net/DelegateCommand.html
The bound component object will show up as the object argument of the delegate.
Update
In the View Model:
public ICommand InstallComponentCommand {get; private set;}
//constructor
{
InstallComponentCommand = new DelegateCommand(p => InstallComponent(p));
}
private void InstallComponent(object data)
{
Component componentToInstall = (Component)data;
// Do whatever is necessary to install
}
As far as disabling the button goes, you could set a property like "InstallInProgress" and bind the "IsEnabled" property of the button to it.
Let me know if I can clarify anything!

WPF Popup like Windows Form

I would like to create a popup in wpf that behaves as windows form. I have manage to close the popup using [x] when it displays. I am stuck on how I can add functional minimize and maximize buttons feature.
here is how i achieved the first step: close button:
How to place close [x] in WPF popup
<Popup Name="myPopup" IsOpen="True">
<StackPanel>
<Label Background="AliceBlue" Foreground="Blue" HorizontalAlignment="Stretch" HorizontalContentAlignment="Right" MouseDown="mouse_DownHandled">
x
</Label>
<Label Name="myLabel" Content="This is a popup!" Background="AliceBlue" Foreground="Blue"/>
</StackPanel>
</Popup>
the mouse_DownHandled event closes the popup by setting isOpen=false
Any ideas?Thanks.
I see what you want. I think what you want is not a Popup Control but actually a WPF Window with a special style set in it.
Use the WindowStyle property to achieve this.
Use
<Window Name="myPopup" WindowStyle="SingleBorderWindow">
<StackPanel>
<Label Name="myLabel" Content="This is a popup!" Background="AliceBlue" Foreground="Blue"/>
</StackPanel>
</Window>
If you want to show this from the code, you can create create an instance of the view and call
window.Show()
Your window should look like this

Problem when setting up command bindings in XAML

I am trying to create a menu bar for my application. I have created a collection in xaml that I will contain menu items that my menu will bind to.
In xaml I have created an array that I use as my static resource for binding.
<coll:ArrayList x:Key="MenuOptionsList">
<model:DashboardMenuBarItem
Icon="the location of an image in my images folder"
DisplayName="The test that will appear under my button"
CommandName="someCommandInMyViewModel"/>
</coll:ArrayList>
I am using a listbox with a data template to show these items as follows.
<ListBox x:Name="lstNavigateTo" MinWidth="400" DockPanel.Dock="Top"
ItemsSource="{Binding Source={StaticResource MenuOptionsList}}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Style="{StaticResource horizontalListTemplate}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<Button Height="60" Width="60"
Command="{Binding Mode=OneWay, Path=CommandName}">
<Button.Content>
<Image Source="{Binding Path=Icon}" Grid.Row="0" />
</Button.Content>
</Button>
<TextBlock Text="{Binding Path=DisplayName}"
Width="100" TextAlignment="Center" Grid.Row="1" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My problem is that I am using the MVVM design pattern and cannot get the command bindings to work on the button click. Previously I would have managed the button click like this.
Command="{Binding someCommandInMyViewModel}"
That would work fine but when I try to bind a command to a property of an item in my collection the command will not fire.
Does anyone know how I can achieve this.
The CommandName property in your collection is of type String, whereas the Command property on Button is of type ICommand. In what way are you expecting WPF to resolve an ICommand from a String? You'll need to help it: either create a converter and use it in your binding, or change your CommandName property so that it contains an actual ICommand rather than a String.

Categories