Usuário com melhor resposta
Event handling usando resource dictionary - window.xaml

Pergunta
-
Boa tarde pessoal!
Estou começando a aprender wpf e estou com uma dúvida.
Gostaria de saber se é possível utilizar o evento do clique do botao usando o resource dictionary para adionar algum evento em um window.xaml.
Ja tentei o seguinte codigo porém nao tive exito:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="NameSpace.MainWindow"
x:ClassModifier="public">Abraço a todos!
Leandro Neroni
Respostas
-
Olá Leandro,
Fiz um exemplo, não sei se é exatamente o que você precisa, mas talvez ajude
o xaml da MainWindow<Window x:Class="WpfApplication4.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"> <Grid> <StackPanel x:Name="panel" Orientation="Vertical"> </StackPanel> </Grid> </Window>
O ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button x:Key="btn" Content="btn in resource dictionary"/> </ResourceDictionary>
o codebehind da MainWindow
Att.public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ResourceDictionary dict = new ResourceDictionary(); dict.Source = new Uri("Dictionary1.xaml", UriKind.Relative); Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(dict); Button btn = (Button)Application.Current.Resources["btn"]; btn.Click += new RoutedEventHandler(btn_Click); Application.Current.Resources["btn"] = btn; this.panel.Children.Add(btn); } void btn_Click(object sender, RoutedEventArgs e) { MessageBox.Show("button click"); } }
Anderson- Marcado como Resposta Adriel CodecoModerator sexta-feira, 4 de março de 2011 04:06
-
Olá Leandro,
Fiz um exemplo, não sei se é exatamente o que você precisa, mas talvez ajude
o xaml da MainWindow<Window x:Class="WpfApplication4.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"> <Grid> <StackPanel x:Name="panel" Orientation="Vertical"> </StackPanel> </Grid> </Window>
O ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button x:Key="btn" Content="btn in resource dictionary"/> </ResourceDictionary>
o codebehind da MainWindow
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ResourceDictionary dict = new ResourceDictionary(); dict.Source = new Uri("Dictionary1.xaml", UriKind.Relative); Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(dict); Button btn = (Button)Application.Current.Resources["btn"]; btn.Click += new RoutedEventHandler(btn_Click); Application.Current.Resources["btn"] = btn; this.panel.Children.Add(btn); } void btn_Click(object sender, RoutedEventArgs e) { MessageBox.Show("button click"); } }
Andersonisso ou use o MVVM e coloque um command no button.
Att,
- Marcado como Resposta Adriel CodecoModerator sexta-feira, 4 de março de 2011 04:06
Todas as Respostas
-
-
Olá Anderson!!
Estou usando o resource dictionary para fazer o menu da minha aplicaçao.
E preciso de um botão para chamar uma função no .cs do xaml.
O resource dictionary tem algumas restriçoes.
Vi em alguns foruns q precisa colocar o x:Class.
Coloquei so que nao deu certo.
Se vc souber de alguma outra maneira, ou me ajudar a identificar o que esta errado agradeço.
Abraços.
Leandro Neroni -
-
-
Olá Leandro,
Fiz um exemplo, não sei se é exatamente o que você precisa, mas talvez ajude
o xaml da MainWindow<Window x:Class="WpfApplication4.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"> <Grid> <StackPanel x:Name="panel" Orientation="Vertical"> </StackPanel> </Grid> </Window>
O ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button x:Key="btn" Content="btn in resource dictionary"/> </ResourceDictionary>
o codebehind da MainWindow
Att.public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ResourceDictionary dict = new ResourceDictionary(); dict.Source = new Uri("Dictionary1.xaml", UriKind.Relative); Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(dict); Button btn = (Button)Application.Current.Resources["btn"]; btn.Click += new RoutedEventHandler(btn_Click); Application.Current.Resources["btn"] = btn; this.panel.Children.Add(btn); } void btn_Click(object sender, RoutedEventArgs e) { MessageBox.Show("button click"); } }
Anderson- Marcado como Resposta Adriel CodecoModerator sexta-feira, 4 de março de 2011 04:06
-
Olá Leandro,
Fiz um exemplo, não sei se é exatamente o que você precisa, mas talvez ajude
o xaml da MainWindow<Window x:Class="WpfApplication4.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"> <Grid> <StackPanel x:Name="panel" Orientation="Vertical"> </StackPanel> </Grid> </Window>
O ResourceDictionary
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button x:Key="btn" Content="btn in resource dictionary"/> </ResourceDictionary>
o codebehind da MainWindow
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ResourceDictionary dict = new ResourceDictionary(); dict.Source = new Uri("Dictionary1.xaml", UriKind.Relative); Application.Current.Resources.MergedDictionaries.Clear(); Application.Current.Resources.MergedDictionaries.Add(dict); Button btn = (Button)Application.Current.Resources["btn"]; btn.Click += new RoutedEventHandler(btn_Click); Application.Current.Resources["btn"] = btn; this.panel.Children.Add(btn); } void btn_Click(object sender, RoutedEventArgs e) { MessageBox.Show("button click"); } }
Andersonisso ou use o MVVM e coloque um command no button.
Att,
- Marcado como Resposta Adriel CodecoModerator sexta-feira, 4 de março de 2011 04:06