CallMethodAction works in MainWindow, but not in UserControl - c#

It works, if I place the code in MainWindow.
It doesn't work, if I place the code in UserControl and then place UserControl
in MainWindow.
I suppose the problem is with binding. How to make it work?
The next works:
MainWindow.xaml:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Name="window" x:Class="WpfApplication1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="Black">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown" SourceName="rectangle">
<ei:CallMethodAction TargetObject="{Binding ElementName=userControl2}" MethodName="Ok"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Rectangle x:Name="rectangle" Width="100" Height="100" Fill="White"></Rectangle>
<local:UserControl2 x:Name="userControl2"></local:UserControl2>
</Grid></Window>
The next doesn't work:
MainWindow.xaml:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Name="window" x:Class="WpfApplication1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="Black">
<Grid>
<local:UserControl1></local:UserControl1>
</Grid></Window>
UserControl1.xaml:
<UserControl 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:WpfApplication1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Name="userControl"
x:Class="WpfApplication1.UserControl1"
mc:Ignorable="d">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown" SourceName="rectangle">
<ei:CallMethodAction TargetObject="{Binding ElementName=userControl2}" MethodName="Ok"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Rectangle x:Name="rectangle" Width="100" Height="100" Fill="White"></Rectangle>
<local:UserControl2 x:Name="userControl2"></local:UserControl2>
</Grid></UserControl>
UserControl2.xaml.cs:
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
public void Ok()
{
MessageBox.Show("a");
}
}

The solution is to move i:Interaction.Triggers below Grid.
Modify UserControl1.xaml to next:
<UserControl 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:WpfApplication1"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Name="userControl"
x:Class="WpfApplication1.UserControl1"
mc:Ignorable="d">
<Grid>
<Rectangle x:Name="rectangle" Width="100" Height="100" Fill="White"></Rectangle>
<local:UserControl2 x:Name="userControl2"></local:UserControl2>
</Grid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown" SourceName="rectangle">
<ei:CallMethodAction TargetObject="{Binding ElementName=userControl2}" MethodName="Ok"/>
</i:EventTrigger>
</i:Interaction.Triggers></UserControl>

Related

How to remove frames, borders around objects in a wpf project

Here on the screens, when I hover the cursor over the object, a gray border appears around itenter image description here
Here
Here
<mah:MetroWindow xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
FontFamily="Century Gothic"
Title="Book Library" Height="660" Width="1250" >
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<WrapPanel>
<Image Width="200" Height="300" Stretch="Fill" Source="{Binding}"/>
<TextBlock Text="{Binding}"/>
</WrapPanel>
</DataTemplate>
</Window.Resources>
</mah:MetroWindow>
Try setting its borderthickness to 0.

C# wpf get rid of margin on top

I am making a mainWindow, the code is as below.
<Window x:Class="ConfigUI.Views.MainUIView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:ConfigUI.Views"
mc:Ignorable="d"
Title="MainUIView" Height="450" Width="800"
FontFamily="Segoe UI" FontSize="14"
WindowStartupLocation="CenterScreen" WindowStyle="None"
>
<DockPanel>
<DockPanel DockPanel.Dock="Top" Margin="0">
<Image Source="..\Assets\logo.png"
Width="50" DockPanel.Dock="Left"
/>
<StackPanel DockPanel.Dock="Right">
<Button FontFamily="Segoe MDL2 Assets" FontSize="24"
Content="" Width="50" Height="50" />
</StackPanel>
<TextBlock Text="My Cloud" FontSize="24" FontWeight="Bold"
HorizontalAlignment="Center" VerticalAlignment="Center"
/>
</DockPanel>
<Grid DockPanel.Dock="Bottom"></Grid>
</DockPanel>
</Window>
But when I run it, it always shows a little gap on the top that I can't get rid of it.
My question is how to remove the gap on the top of the window? Thanks.
(Solution 1) This way you will loose drop shadow.
You just set AllowsTransparency="True" in your Window Code. This will remove visible border from window.
<Window x:Class="ConfigUI.Views.MainUIView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:ConfigUI.Views"
mc:Ignorable="d"
Title="MainUIView" Height="450" Width="800"
FontFamily="Segoe UI" FontSize="14"
WindowStartupLocation="CenterScreen" WindowStyle="None"
AllowsTransparency="True">
</Window>
(Solution 2) Drop shadow will intact.
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0" ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>
Full Codes
<Window x:Class="SOWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SOWPF"
mc:Ignorable="d" WindowStyle="None"
Title="MainWindow" Height="450" Width="800">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="0" ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>
</Window>
Tip*
Add ResizeMode="CanResize"
Try this:
<DockPanel VerticalAlignment="Top">
or this
<DockPanel Margin="0,-4,0,0">

The Heading2 style in ModernUI

We're working with the ModernUI framework for our WPF apps. I've come across a style called Heading2, but I cannot find it anywhere in our code, either the XAML or the C# code. Is Heading2 defined somewhere in ModernUI?
Yes, you can reference it in your code as below:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/FirstFloor.ModernUI;component/Assets/TextBlock.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Hello Heading2!" Style="{StaticResource Heading2}"></TextBlock>
<TextBlock Text="No Heading..."></TextBlock>
</StackPanel>
</Grid>

Only see the first referenced UserControl

How it is possible that only the first UserControl ni the WindowsFormsHost is shown in the GUI while the seconde one is still hidden?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf_zoom0="clr-namespace:LowLightGrab;assembly=LowLightGrab"
xmlns:wf_zoom1="clr-namespace:LowLightGrab;assembly=LowLightGrab"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WindowsFormsHost Height="154" HorizontalAlignment="Left" Name="wf_zoom0" VerticalAlignment="Top" Width="215" >
<wf_zoom0:UserControl1/>
</WindowsFormsHost>
<WindowsFormsHost Height="161" HorizontalAlignment="Left" Margin="251,138,0,0" Name="wf_zoom1" VerticalAlignment="Top" Width="223" >
<wf_zoom1:UserControl1/>
</WindowsFormsHost>
</Grid>
Other than removing the margin as Eran added, You should add Rows to the Grid and place each of the elements in its own row, something lile this:
<Window x:Class="ComboboxRectangles.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf_zoom0="clr-namespace:LowLightGrab;assembly=LowLightGrab"
xmlns:wf_zoom1="clr-namespace:LowLightGrab;assembly=LowLightGrab"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.RowDefinitions>
<WindowsFormsHost Grid.Column="0" Height="154" HorizontalAlignment="Left" Name="wf_zoom0" VerticalAlignment="Top" Width="215" >
<wf_zoom0:UserControl1/>
</WindowsFormsHost>
<WindowsFormsHost Grid.Column="1" Height="161" HorizontalAlignment="Left" Margin="251,138,0,0" Name="wf_zoom1" VerticalAlignment="Top" Width="223" >
<wf_zoom1:UserControl1/>
</WindowsFormsHost>
</Grid>
</Window>

Host TabItem from Another Assembly

I am trying to create a user control which is basically include a Tab Item. As follows and try to add it to my tab control in another library.
//Grid.xaml in a.dll
<UserControl x:Name="Grid" x:Class="SomeClass"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabItem Header="Grid">
<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</TabItem>
</UserControl>
//TabView.xaml in b.dll
<UserControl
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:Views="clr-namespace:SomeClass;assembly=SomeAssembly" x:Class="SomeClass"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Views:GridView/>
<TabItem Header="This" />
<TabItem Header="That" />
</TabControl>
</UserControl>
My problem is that, it actually creates the tab there, but does not show the header of tab. I wonder if I am doing it right and how can I show the header?
Your problem is that your control in a.dll is not TabItem. It is UserControl. You can either inherit it from TabItem (it`s a bad way for WPF) or use composition:
//Grid.xaml in a.dll
<UserControl x:Name="Grid" x:Class="SomeClass"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</UserControl>
//TabView.xaml in b.dll
<UserControl
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:Views="clr-namespace:SomeClass;assembly=SomeAssembly" x:Class="SomeClass"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabControl HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<TabItem Header="Grid">
<Views:GridView/>
</TabItem>
<TabItem Header="This" />
<TabItem Header="That" />
</TabControl>
</UserControl>

Categories