Call RelayCommand in WPF-Control from WPF-Windows (XAML) [closed] - wpf

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a WPF UserControl with a ViewModel as DataContext. This ViewModel also contains RelayCommands. From the control I can call these commands (e.g. with a button) <Button Command="{Binding SaveCommand}">Save</Button>. Now I want to place the control in a window and call the commands from there <Button Command="{Binding SaveCommand}" CommandTarget="{Binding ElementName=myControl}">Save</Button>. But this does not work. How can I call a command in a child control in XAML?
Thanks a lot
Markus

If your main window has different DataContext and you can't reach this command through it you can write
<Button Command="{Binding DataContext.SaveCommand, ElementName=myControl}">Save</Button>

Related

WPF Custom Textbox control within UserControl RelativeSource causing TextChanged event to fire even after loaded [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
I have a custom user control defined in XAML like this:
<UserControl x:Class="MyUserControl"
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:MyApplicationName"
xmlns:wpf="clr-namespace:LibVLCSharp.WPF;assembly=LibVLCSharp.WPF"
mc:Ignorable="d"
>
Within it, I have a custom textbox control, called CustomIndicator which inherits TextBox in code-behind, placed in XAML like this:
<local:CustomIndicator x:Name = "Parameter1" Background="{Binding ElementName=Parameter1, Path=OffColor}" Text="{Binding myUserParameter1, StringFormat=N1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}" OnText="1" OffText="1" ModuleKey="D1" ModuleName="1" Height="20"/>
After the user control is loaded, in Me.Loaded, I loop through all of the controls and add a TextChanged handler to the CustomIndicator (recall it is a custom Textbox control) like this:
AddHandler TryCast(ctrl, TextBox).TextChanged, Sub(sender, e) ControlValueChanged(sender, e)
I'm positive it's being added after both A) the user control is initialized/loaded and B) the custom textbox control, CustomIndicator, is initialized/loaded...I put a breakpoint at the adding of the handler and confirmed both are IsInitialized/IsLoaded=True. However, the TextChanged event is still firing because it appears that the binding is still occurring yet later. If I change the XAML to this, it doesn't fire, but I also don't get the value:
<local:CustomIndicator x:Name = "Parameter1" Background="{Binding ElementName=Parameter1, Path=OffColor}" Text="{Binding myUserParameter1, StringFormat=N1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" OnText="1" OffText="1" ModuleKey="D1" ModuleName="1" Height="20"/>
The removal of the "RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}" makes the TextChanged stop firing late, but I also don't get the value. How can I add the handler such that it is, truly, after everything is loaded, including the binding?
Running a trace, by the way, did reveal the following:
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=myUserParameter1; DataItem=null; target element is 'CustomIndicator' (Name='Parameter1'); target property is 'Text' (type 'String')
Don't know if the trace helps...but I'll also mention that myUserParameter1 holds the value of 0 (or some other numeric value, which is set in the Loaded="Window_Loaded" event of the main window), and the textbox is still blank by the end of the Me.Loaded event.
Though it isn't really solved, I found a solution that works. The two things I did were 1) added a FallbackVaue to my bindings, and 2) changed the order of the user control instance creation and update of values. While those two things don't fully explain what was going on, they're good enough to provide the needed functionality.

Add multiple buttons [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I want to add x buttons this value is going to be from a variable that is inputed by the user
I put like 5 tables and 5 buttons will appear.
Any help?
Sounds like you want an ItemsControl. It let's you define a template (in your case, a button) and bind to a collection of items like this:
<ItemsControl ItemsSource={Binding MyCollectionOfObjects>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding MyStringProperty}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

WPF listBox collection slection changes are not working propely [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to load a collection to listbox as follows.(one item in listbox contains a image and a text block) IF i click each item by item in outside of the image( area 2) selection change working fine(highlight correctly); but if do the selection change by clicking each image this want work correctly.What is the reason behind this?
item by item
<DataTemplate x:Key="ObjectGalleryDataTemplate" DataType="{x:Type loc:ObjectTypes}" >
<Button Margin="3" Width="80" Height="80" Click="click_object"BorderBrush="Transparent" Tag="{Binding ObjectTypeID}">
<ItemsPanelTemplate x:Key="ObjectGalleryItemsPanelTemplate">
<UniformGrid Rows="1" HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
<StackPanel Orientation="Horizontal">
<Grid><ListBox x:Name="ObjectTypesGallery" SelectionMode="Single" SelectionChanged="objectType_clik" BorderBrush="Transparent" SelectedIndex="0" ItemsSource="{Binding}" ItemTemplate="{DynamicResource ObjectGalleryDataTemplate}" ItemsPanel="{DynamicResource ObjectGalleryItemsPanelTemplate}">
</ListBox>
</Grid>
</StackPanel>
Since you asked:
what is the reason behind this?
The reason is that each item in a list box is automatically made selectable (so that you can choose one or more list items as part of your UI). But since you have included a button in your list item template, when you click on the button, the system is responding to the button being clicked first, in front of the list item.
You didn't specify how you want this to behave. I would guess that you should either use an <ItemsControl> if you don't want to be able to select individual items, or change your <Button> to an <Image> or something similar if you don't want to be able to click the icon in the center. Or, if you want both click events to fire (the button click, and the list item selection click), you will need to implement this manually using event handlers.

Find control in templated WPF datagrid [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
get value of checkbox from datagrid? C#
I am trying to find a control inside the selected row in a templated DataGrid.
<DataGridTemplateColumn Header="Local">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkImport" IsChecked="{Binding IsLocalized}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I am trying the following code:
var selectedRow = (DataGridRow) gridFileScan.ItemContainerGenerator.ContainerFromItem(gridFileScan.SelectedItem);
CheckBox chkImport = FindVisualChild<CheckBox>(selectedRow);
but chkImport is always null. Any ideas ??
When you debug you should be able to see the method recurse the VisualTree.
You can see the Visual Tree by using the Visual Tree Visualizer
The implementation of FindVisualChild might be flawed or the VisualTree doesn't look like what you expect.
Found it. I just had to call this method after modifying ItemsSource:
gridFileScan.UpdateLayout();

Use Ribbon Control in WPF Toolkit in XAML [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
How to use the Ribbon control in WPf toolkit in my XAML?
How to include the namespaces for ribbon in XAML?
Good link Trainee4Life, just like this one.
As per the walkthrough:
<r:RibbonTab Label="Banking">
<r:RibbonGroup>
<r:RibbonButton Command="me:AppCommands.Cut"/>
<r:RibbonButton Command="me:AppCommands.Copy"/>
<r:RibbonButton Command="me:AppCommands.Paste"/>
</r:RibbonGroup>
<r:RibbonGroup>
<r:RibbonButton Command="me:AppCommands.AddNew"/>
<r:RibbonButton Command="me:AppCommands.Clear" />
<r:RibbonButton Command="me:AppCommands.Delete"/>
</r:RibbonGroup>
<r:RibbonGroup>
<r:RibbonButton Command="me:AppCommands.DownloadStatements"/>
<r:RibbonButton Command="me:AppCommands.DownloadCreditCards"/>
<r:RibbonButton Command="me:AppCommands.Transfer"/>
</r:RibbonGroup>
</r:RibbonTab>
I did work with Ribbon for a considerable amount of time. Nothing beats these tutorials.
http://www.uxpassion.com/2009/08/wpf-ribbon-control-roadmap-and-look-into-the-future/
And for the namespace problem, include a reference to RibbonControlsLibrary.dll in your project, and use the following line to point to the namespace.
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"

Resources