Navigation in C# XAML Not Working - c#

Foreword: I'm posting this here as I didn't get any replies to my post here: https://social.msdn.microsoft.com/Forums/vstudio/en-US/5d7d4554-7d4b-45af-b02c-22ed0c7695a2/navigation-in-c-xaml-not-working?forum=vsta
I know Stackoverflow is a much more reliable source, so I decided to repost it here.
I'm trying to make make my first app in VS, and I want it to just be an informational App about cubing (Solving Rubik's style cubes very fast.) I am just learning the basics of C# and XAML at the moment, but am unable to get navigation between pages. All tutorials I have seen say use the line of code:
this.Frame.Navigate(typeof(PLL), null);
But it gives me this error:
'Mainwindow' does not contain a definition for 'Frame' and no extension method 'Frame' accepting a first argument of type 'MainWindow' could be found (are you missing a using directive or an assembly reference?
I also want to point out that they say to use the 'Blank App' Template, but I can't seem to find that - is it not in VS community? Instead, I had to use the WPF app template.
What am I doing wrong? How can I get these links between pages working?
Below is the whole of my C# and XAML code.
Thanks!
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace CubingGuide
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void GoToPLL(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(PLL), null);
}
}
}
XAML:
<Window x:Class="CubingGuide.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:CubingGuide"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="GoToPLLButt" Margin="10,10,360,251" Content="PLL" Click="GoToPLL"/>
</Grid>
</Window>

You need to create Frame object in XAML if you want to navigate to the Frame(i.e. you need to populate the frame with your "PLL").

you need a Frame control in MainPage.xaml
<Window x:Class="WpfApplication1.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:WpfApplication1"
xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="60" Margin="64,89,0,0" VerticalAlignment="Top" Width="135" Click="button_Click"/>
<Controls:Frame Name="MainFrame" NavigationUIVisibility="Hidden" >
</Controls:Frame>
</Grid>
</Window>
On click event you just need to add this code:
private void button_Click(object sender, RoutedEventArgs e)
{
MainFrame.Navigate(new Page1());
}
I hope this helps :)

<Custom:Ribbon x:Name="ribbon" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Width="934">
<Custom:Ribbon.QuickAccessToolBar>
<Custom:RibbonQuickAccessToolBar>
<Custom:RibbonQuickAccessToolBar>
<Custom:RibbonSplitMenuItem Header="مرحله سوم"/>
</Custom:RibbonQuickAccessToolBar>
</Custom:Ribbon>
</Grid>

Related

Oxyplot application - Cannot find referencing error in xaml file

Am new to oxyplot and wish to use it with C# WPF with xaml. My .xaml file has the following code:
<code>
<Window x:Class="CurveMaster.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:CurveMaster"
xmlns:oxy="http://oxyplot.org/wpf"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<oxy:Plot Title="{Binding Title}">
<oxy:Plot.Series>
<oxy:LineSeries ItemsSource="{Binding Points}"/>
</oxy:Plot.Series>
</oxy:Plot>
</Grid>
</Window>
</code>
The .xaml.cs file is as follows:
<code>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using OxyPlot;
namespace CurveMaster
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public class MainViewModel
{
public MainViewModel() //Constructor
{
this.Title = "My Graph";
this.Points = new List<DataPoint>
{
new DataPoint (0,4),
new DataPoint (10,13),
new DataPoint (20,15),
};
}
public string Title { get; private set; }
public IList <DataPoint> Points { get; private set; }
}
}
</code>
So I'm just trying to run the above simple example but get the following error messages in the xaml file:
The type 'oxy:Plot' was not found. Verify that you are not missing an assembly reference.
The name "MainViewModel" does not exist in the namespace "clr-namespace:CurveMaster".
Now I installed the oxyplot Nuget package for WPF and installation was successful. However, above tells me that the above two issues are both referencing issues but I cannot figure out what the problem is. What additional oxyplot referencing is required once the package is installed?
Secondly why does it give me the MainViewModel does not exist message when in fact it does exist in the CurveMaster namespace?
Thanks in advance.
If you (or someone else) still needs the answer - thats what helped me.
I also tried to install OxyPlot from Nuget and it gave referencing and non-existing errors. So I finally overpowered it with:
I have downloaded oxyplot from github, built it and took necessary DLL manually into my solution (OxyPlot.dll, OxyPlot.wpf.dll, OxyPlot.wpf.shared.dll)
I have changed
xmlns:oxy="http://oxyplot.codeplex.com"
to
xmlns:oxy="http://oxyplot.org/wpf"
in XAML.

WPF C# user control with 2 or more buttons

Sorry if this has been asked before and I have spent about a week trying to find a similar question to point me in the right direction. I am teaching myself C# with WPF, XAML etc and am playing around with user controls. I made a simple app with a user control to load on top of other windows or user controls. The UC in question has two buttons and I need to get to the click events for each button in main window once the control is loaded. The main window has a button that loads the control.
Through some research I was able to find a solution from user SWilko (https://stackoverflow.com/a/28949666/10659981) but I can't figure it out for each button separately (click button a and show "clicked btn a", click button b and show "clicked button b"). I did try calling by sender using name and that will not work either. I feel like I am close with the help from the answer by SWilko but stuck.
Here is the code so far:
Basic main screen loading user control
<Window x:Class="UCBTN_TEST.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:UCBTN_TEST"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="435">
<Grid>
<Button Content="Load Button" HorizontalAlignment="Left" Margin="18,23,0,0" VerticalAlignment="Top" Width="74" Click="Button_Click"/>
<Grid x:Name="GridLoad" HorizontalAlignment="Left" Height="300" Margin="120,23,0,0" VerticalAlignment="Top" Width="300" Background="#FFF1CBCB"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UCBTN_TEST
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
GridLoad.Children.Clear();
GridLoad.Children.Add(new WindowControl());
}
}
}
The button user control
<UserControl x:Name="UCMain" x:Class="UCBTN_TEST.Controls.ButtonControl"
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:UCBTN_TEST.Controls"
mc:Ignorable="d" d:DesignWidth="300" Height="40.333">
<Grid Background="#FFE7EEA7">
<Button x:Name="ButtonA" Content="Button A" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonA_Click" Background="Red"/>
<Button x:Name="ButtonB" Content="Button B" HorizontalAlignment="Left" Margin="215,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonA_Click" Background="Green"/>
</Grid>
</UserControl>
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace UCBTN_TEST.Controls
{
/// <summary>
/// Interaction logic for ButtonControl.xaml
/// </summary>
public partial class ButtonControl : UserControl
{
public ButtonControl()
{
InitializeComponent();
}
private void ButtonA_Click(object sender, RoutedEventArgs e)
{
RaiseEvent(new RoutedEventArgs(ClickEvent1, this));
}
public static readonly RoutedEvent ClickEvent1 = EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ButtonControl));
public event RoutedEventHandler Click
{
add { AddHandler(ClickEvent1, value); }
remove { RemoveHandler(ClickEvent1, value); }
}
}
}
A second user control which would ultimately have some other controls once the buttons work correctly. But the button UC will load on top, simple button features related to WindowControl.
<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:UCBTN_TEST"
xmlns:Controls="clr-namespace:UCBTN_TEST.Controls" x:Class="UCBTN_TEST.WindowControl"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="#FFE7CFEE">
<Controls:ButtonControl HorizontalAlignment="Left" Height="37" VerticalAlignment="Top" Width="300" Click="Click1"/>
</Grid>
</UserControl>
I understand the behind code and why this is happening. My problem is that I need to have the buttons be unique in their events. I have tried calling by sender and name and that just kills the event all together.
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCBTN_TEST.Controls;
namespace UCBTN_TEST
{
/// <summary>
/// Interaction logic for WindowControl.xaml
/// </summary>
public partial class WindowControl : UserControl
{
public WindowControl()
{
InitializeComponent();
}
private void Click1(object sender, RoutedEventArgs e)
{
MessageBox.Show("This triggers both");
}
}
}
I was going to add a bunch of comments but really this is kind of answering the question and there's a lot to explain.
You should look into MVVM and mostly be thinking in terms of binding commands rather than which button was clicked. There are exceptions to this. For example, if you were building an on screen keyboard. The reason this is different because it's purpose can be encapsulated. The user presses a button which has "A" in it. Whatever textbox is focussed should be sent the character "A". They press a button showing "B" and similarly "B" should be sent. That functionality can be encapsulated in the control.
As it is, you have two buttons.
You put them in a usercontrol and encapsulate them.
By doing this you created a boundary.
This then creates a complication - which was clicked?
The usercontrol is also not particularly re-use friendly. If you add two then there are two buttonA and two button B. You could potentially improve that with a custom event args on your custom routed event and a dependency property on your usercontrol. Pass some usercontrol identifier along with which button was pressed.
This would be an unusual way to work though. I've rarely seen Custom routed events used in commercial apps.
All in all I would suggest the usercontrol mainly adds complexity.
Say you wanted to have 20 sets of 2 buttons.
Or 20 sets of 5 radiobuttons for a set of multiple choice questions.
The way to do that sort of thing is to use an itemscontrol and template out the multiple controls. One template giving 2 buttons ( or a textblock question and 5 radiobuttons for answers ) per row.
A click event is already a routed event and would bubble to the window. You may as well remove your custom routed event and the handler out the usercontrol... and the usercontrol. Just handle click in the window.
Code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var btn = e.OriginalSource as Button;
if(btn == null)
{
return;
}
MessageBox.Show($"Button clicked was {btn.Tag}");
}
Markup:
ButtonBase.Click="Button_Click"
Title="MainWindow"
>
<Grid>
<StackPanel>
<Button x:Name="ButtonA" Content="Button A" Tag="A" Background="Red"/>
<Button x:Name="ButtonB" Content="Button B" Tag="B" Background="Green"/>
</StackPanel>
</Grid>
</Window>

How to create a Visual Studio 2015 Startpage with WPF?

I need a little kickstart for creating a Visual Studio "Startpage" in the Version 2015.
There are old information (https://msdn.microsoft.com/en-us/library/Ee663382(v=vs.120).aspx) out there and some projects on Visual Studio Galery (eg "BetterStartPage" or "SolutionStartPage". Expecialy the "Custom Start Page Project Template" is out of date :-(
I do start a project with this tutorial: https://msdn.microsoft.com/en-us/library/ff425532(v=vs.140).aspx
But the steps and the information are quite ... short.
Here is my problem:
I compile the Custom Control Library project and following the manual installation steps mention in the article. I can select my custom start page, but it doesn't start my custom contol. The page is just empty. Its like the code in the DLL is not executed.
Here is the test-XAML file:
<UserControl
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:MyNamespace="clr-namespace:StartPageControl;assembly=StartPageControl"
xmlns:vs="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.14.0"
xmlns:vsfx="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.14.0"
xmlns:MeineControls="clr-namespace:StartPageControl"
xmlns:local="clr-namespace:StartPageControl"
x:Class="StartPageControl.UserControl1"
mc:Ignorable="d"
Height="350" Width="525">
<Grid>
<Grid x:Name="LayoutGrid" >
<Button Click="Button_Click" Content="push me" Foreground="black" />
<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="720,171,-295,0" VerticalAlignment="Top" Width="100"/>
</Grid>
</Grid>
</UserControl>
UserControl1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace StartPageControl
{
/// <summary>
/// Interaktionslogik für UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl {
public UserControl1()
{
InitializeComponent();
ButtonApp ButtonApp1 = new ButtonApp();
LayoutGrid.Children.Add(ButtonApp1 as UIElement);
}
private void Button_Click(object sender, RoutedEventArgs e) {
label.Content = "Something happend!";
}
}
}
EDIT:
Here are the steps I did:
Creating a new WPF Control Libraby Project
Add a new XAML and add/change the namespaces
Add some "code behind" eg a Button with a Click-Event
Compile
Copy the XAML file in %USERPROFILE%\My Documents\Visual Studio 2015\StartPages\
Copy the Project .DLL in \Common7\IDE\PrivateAssemblies\
Start a new VS and choose the new Startpage
Getting the error (translated from german):
"System.Windows.Markup.XamlParseException" ... "Error creating 'Click' from Text 'Button_Click'"
..so I tried to use a UserControl in a UserControl (as you can see in the code above). There comes the the message "{clr-namespace:StartPageControl}ButtonApp can not be created"
:-( It seems to me, VS don't use the "Code Behind"/DLL...
I know it's a kind of diffuse question. Knows anybody a better startingpoint?
EDIT 2:
Paste all the Test-Code.

Visual Studio - WPF - References not adding properly

I have looked through past questions on this issue to no avail.
I've just created a new WPF project in VS 2013. I go to Add Reference, and select System.Windows.Form. It adds. Great!
However, the appropriate tools are still greyed out in the toolbox. Yes, auto-update toolbox is on. I've shown all. I've restarted VS and rebuilt my solution. I've added using System.Windows.Forms; to my MainWindow.xaml.cs file.
At this, I only have the bare bones code because this is a completely new project that I haven't touched yet.
What am I missing?? I've tried dragging the .dll file to the toolbox, tools are still greyed. Is there a piece of code I'm missing somewhere?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
namespace sub20tool3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
And:
<Window x:Class="sub20tool3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:local="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525">
<Grid>
</Grid>
</Window>
You need a WindowsFormHost element to hold winforms controls in a WPF project. The WindowsFormHost also needs a reference to WindowsFormIntegration.
Here's a good tutorial on how to use them: http://www.wpf-tutorial.com/misc-controls/the-windowsformshost-control/
<Window x:Class="WpfTutorialSamples.Misc_controls.WindowsFormsHostSample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="WindowsFormsHostSample" Height="350" Width="450">
<Grid>
<WindowsFormsHost Name="wfhSample">
<WindowsFormsHost.Child>
<wf:WebBrowser DocumentTitleChanged="wbWinForms_DocumentTitleChanged" />
</WindowsFormsHost.Child>
</WindowsFormsHost>
</Grid>
</Window>

How can the opacity of a program be controled through a settings window in real time?

I am trying to change the Opacity of the main application window through a settings window popup in real time. What is the proper way of doing this?
So far, I have tried using a Slider to output the value to the settings file. When the settings popup is closed, the main window refreshes its opacity property based on the settings file. This method works but I'd like the ability to change the opacity and view the result in real time.
The second method I tried was using a Style and applying it to the MainWindow. Then on slider move, the style would be overridden with the value from the slider. This works in real time. But for whatever reason, the settings popup window opacity is also affected even though no style is applied to it.
Here is a sample project named OpacityTest with a main window, a button to open settings popup and slider to control the program's opacity.
App.xaml:
<Application x:Class="OpacityTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="Window" x:Key="wrapper">
<Setter Property="OverridesDefaultStyle" Value="false"/>
<Setter x:Name="opacitySetter" Property="Opacity" Value="1"/>
</Style>
</Application.Resources>
</Application>
MainWindow.xaml:
<Window x:Class="OpacityTest.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" Style="{DynamicResource wrapper}" Background="#FFCDCDCD" AllowsTransparency="True" WindowStyle="None">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="Settings" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>
New window labeled Settings, Settings.xaml:
<Window x:Class="OpacityTest.Settings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Settings" Height="300" Width="300">
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Slider x:Name="ChangeTransparency" MinWidth="138" MinHeight="22" VerticalAlignment="Center" Padding="0" Orientation="Horizontal" HorizontalAlignment="Center" Value="1" Minimum=".05" Maximum="1" LargeChange=".01" SmallChange=".01" TickFrequency="100" IsSnapToTickEnabled="False" MouseMove="ChangeTransparency_MouseMove"/>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace OpacityTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Settings settings = new Settings();
settings.ShowDialog();
}
}
}
Settings.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace OpacityTest
{
/// <summary>
/// Interaction logic for Settings.xaml
/// </summary>
public partial class Settings : Window
{
public Settings()
{
InitializeComponent();
}
private void ChangeTransparency_MouseMove(object sender, MouseEventArgs e)
{
Style style = new Style { TargetType = typeof(Window) };
style.Setters.Add(new Setter(OpacityProperty, Opacity = ChangeTransparency.Value));
Application.Current.Resources["wrapper"] = style;
}
}
}
It looks like your Style is behaving as if it were declared in a XAML file in a way that affects all Windows, not just the wrapper style, e.g.
<Style TargetType="Window"> ... </Style>
Instead of:
<Style TargetType="Window" x:Key="wrapper"> ... </Style>
I'm not immediately sure of how to declare it the way you're trying to, but a much easier way to accomplish what you're trying to do is to put this in MainWindow.xaml.cs:
private void Button_Click(object sender, RoutedEventArgs e)
{
Settings settings = new Settings();
this.SetBinding(OpacityProperty,
new Binding("Value") { Source = settings.ChangeTransparency });
settings.ShowDialog();
}
No handler on ChangeTransparency is required this way. The only reason I might consider modifying the Style instead is if you can have multiple MainWindows at once, and want one Settings window (opened from any of them) to control them all at once.
As an aside, if you do find that you need to attach to ChangeTransparency, you should attach a handler to its ValueChanged event instead of MouseMove (while MouseMove will generally work out, it's really not the same; e.g. for keyboard input, only ValueChanged will fire).
If Settings can be used from other windows and/or has more properties, you might wish to change Settings to take a Window (or other type) in its constructor, and set up the binding(s) there instead, to keep your code/logic centralized and clean.

Categories

Resources