how can I set the style in app.xaml to my button in c# ?
there is a style for buttons in app.xaml and I want to use it in my button in c#.
here is my code for the button.
Button deleteButton = new Button();
deleteButton.Background = new ImageBrush
{
ImageSource ="Delete2.png"
};
deleteButton.Height = 70;
deleteButton.Width = 70;
deleteButton.HorizontalAlignment = HorizontalAlignment.Left;
deleteButton.VerticalAlignment = VerticalAlignment.Bottom;
deleteButton.Margin = new Thickness(0,10,0,-5);
deleteButton.BorderBrush = null;
deleteButton.Visibility = Visibility.Collapsed;
deleteButton.Click += new RoutedEventHandler(ItemDeleteButton_Click);
deleteButton.Tag = tag;
deleteButton.Style = ?????????????????
Try this code
deleteButton.Style = App.Current.Resources["StyleKey"] as Style;
You can use FindResource or TryFindResource.
Resources and Code - MSDN
I solved it
deleteButton.Style = App.Current.Resources["ButtonStyle"] as Style;
Your Style is probably defined in a ResourceDictionary elsewhere in your application. You can retrieve that using the FindResource method:
deleteButton.Style = (Style)this.FindResource("MyCustomButtonStyle");
Related
There are 2 part for setting style of button.
The 1st is BackgroundProperty by Style.Setters. It's working well.
The 2nd is BackgroundProperty by Style.Triggers which is run by that Mouse is over a button. But, It's not working.
// the 1st BackgroundProperty by Style.Setters
Style style = new Style(typeof(Button));
style.Setters.Add(new Setter(Button.BackgroundProperty, imageSourceOn));
// the 2nd BackgroundPropert by Style.Triggers
Setter setter = new Setter();
setter.Property = Button.BackgroundProperty;
setter.Value = imageSourceOff;
Trigger trigger = new Trigger();
trigger.Property = IsMouseOverProperty;
trigger.Value = true;
trigger.Setters.Add(setter);
style.Triggers.Add(trigger);
Button button = new Button();
button.Margin = new Thickness(0, 5, 80, 5);
button.BorderThickness = new Thickness(0);
button.Name = name;
button.Click += handler;
button.VerticalContentAlignment = VerticalAlignment.Bottom;
// setting Style
button.Style = style;
return button;
Thanks Ed Plunkett.
I added ControlTemplate to override the control's background brush property.
Style style = new Style(typeof(Button));
style.Setters.Add(new Setter(Button.BackgroundProperty, imageSourceOn));
This is the ControlTemplate part what I add:
ControlTemplate template = new ControlTemplate(typeof(Button));
FrameworkElementFactory elemFactory = new FrameworkElementFactory(typeof(Border));
elemFactory.Name = "Border";
elemFactory.SetValue(Border.BackgroundProperty, new TemplateBindingExtension(Button.BackgroundProperty));
template.VisualTree = elemFactory;
elemFactory.AppendChild(new FrameworkElementFactory(typeof(ContentPresenter)));
And, It is trigger part.
Setter setter = new Setter();
setter.Property = Button.BackgroundProperty;
setter.Value = imageSourceOff;
Trigger trigger = new Trigger();
trigger.Property = IsMouseOverProperty;
trigger.Value = true;
trigger.Setters.Add(setter);
Finally, I set my ControlTemplate.
template.Triggers.Add(trigger);
setter = new Setter();
setter.Property = Button.TemplateProperty;
setter.Value = template;
style.Setters.Add(setter);
It works well.
I have been able to do the above with the following code in my subclassed TabItem:
protected override void OnSelected(RoutedEventArgs e)
{
base.OnSelected(e);
if (this.StoryBoard == null)
{
ColorAnimation anim = new ColorAnimation(Colors.Transparent, Colors.AliceBlue, new Duration(TimeSpan.FromSeconds(1)))
{
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
};
Storyboard.SetTarget(anim, this);
Storyboard.SetTargetProperty(anim, new PropertyPath("Background.Color"));
Storyboard sb = new Storyboard();
sb.Children.Add(anim);
this.StoryBoard = sb;
}
VisualTree.FindParent<OMWTabControl>(this).Items.Cast<OMWTabItem>().ToList().ForEach(n =>
{
if (n.StoryBoard != null)
{
n.StoryBoard.Stop();
}
});
this.StoryBoard.Begin();
}
I know I am butchering the correct way to do this - I should be using DataTriggers I believe with Setters.
I have done extensive searching and it's all in XAML which is mostly intrepretable into C#, but not all.
Can someone point me to the "correct" way to do this in code behind?
Whenever you want to know about any methods, interfaces, classes, or just about anything about the .NET Framework, just go to MSDN. You could have gone to the Storyboard.TargetProperty Attached Property page on MSDN for a super quick answer to your question. From the linked page:
public StoryboardExample()
{
// Create a name scope for the page.
NameScope.SetNameScope(this, new NameScope());
this.WindowTitle = "Animate Properties using Storyboards";
StackPanel myStackPanel = new StackPanel();
myStackPanel.MinWidth = 500;
myStackPanel.Margin = new Thickness(30);
myStackPanel.HorizontalAlignment = HorizontalAlignment.Left;
TextBlock myTextBlock = new TextBlock();
myTextBlock.Text = "Storyboard Animation Example";
myStackPanel.Children.Add(myTextBlock);
//
// Create and animate the first button.
//
// Create a button.
Button myWidthAnimatedButton = new Button();
myWidthAnimatedButton.Height = 30;
myWidthAnimatedButton.Width = 200;
myWidthAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left;
myWidthAnimatedButton.Content = "A Button";
// Set the Name of the button so that it can be referred
// to in the storyboard that's created later.
// The ID doesn't have to match the variable name;
// it can be any unique identifier.
myWidthAnimatedButton.Name = "myWidthAnimatedButton";
// Register the name with the page to which the button belongs.
this.RegisterName(myWidthAnimatedButton.Name, myWidthAnimatedButton);
// Create a DoubleAnimation to animate the width of the button.
DoubleAnimation myDoubleAnimation = new DoubleAnimation();
myDoubleAnimation.From = 200;
myDoubleAnimation.To = 300;
myDoubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(3000));
// Configure the animation to target the button's Width property.
Storyboard.SetTargetName(myDoubleAnimation, myWidthAnimatedButton.Name);
Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Button.WidthProperty));
// Create a storyboard to contain the animation.
Storyboard myWidthAnimatedButtonStoryboard = new Storyboard();
myWidthAnimatedButtonStoryboard.Children.Add(myDoubleAnimation);
// Animate the button width when it's clicked.
myWidthAnimatedButton.Click += delegate(object sender, RoutedEventArgs args)
{
myWidthAnimatedButtonStoryboard.Begin(myWidthAnimatedButton);
};
myStackPanel.Children.Add(myWidthAnimatedButton);
//
// Create and animate the second button.
//
// Create a second button.
Button myColorAnimatedButton = new Button();
myColorAnimatedButton.Height = 30;
myColorAnimatedButton.Width = 200;
myColorAnimatedButton.HorizontalAlignment = HorizontalAlignment.Left;
myColorAnimatedButton.Content = "Another Button";
// Create a SolidColorBrush to paint the button's background.
SolidColorBrush myBackgroundBrush = new SolidColorBrush();
myBackgroundBrush.Color = Colors.Blue;
// Because a Brush isn't a FrameworkElement, it doesn't
// have a Name property to set. Instead, you just
// register a name for the SolidColorBrush with
// the page where it's used.
this.RegisterName("myAnimatedBrush", myBackgroundBrush);
// Use the brush to paint the background of the button.
myColorAnimatedButton.Background = myBackgroundBrush;
// Create a ColorAnimation to animate the button's background.
ColorAnimation myColorAnimation = new ColorAnimation();
myColorAnimation.From = Colors.Red;
myColorAnimation.To = Colors.Blue;
myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(7000));
// Configure the animation to target the brush's Color property.
Storyboard.SetTargetName(myColorAnimation, "myAnimatedBrush");
Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
// Create a storyboard to contain the animation.
Storyboard myColorAnimatedButtonStoryboard = new Storyboard();
myColorAnimatedButtonStoryboard.Children.Add(myColorAnimation);
// Animate the button background color when it's clicked.
myColorAnimatedButton.Click += delegate(object sender, RoutedEventArgs args)
{
myColorAnimatedButtonStoryboard.Begin(myColorAnimatedButton);
};
myStackPanel.Children.Add(myColorAnimatedButton);
this.Content = myStackPanel;
}
Please visit the linked page for further information.
When i add a radio button in visual c# the text name of the radio button is aligned perfectly to the right . if i create a radio button dynamically and give it several properties etc.. when i debug and view it, the text or name of the radio button is shifted slighted up and to the right of the radio button? i am looked over several properties including padding etc.. but i am not able to figure out how to correct this dynamically. what is going on and how can i fix this?
here is example of the properties i am using right now
radio_ip_addresses[i] = new RadioButton();
radio_ip_addresses[i].Name = "radio_" + i;
radio_ip_addresses[i].Text = ip_addresses.Dequeue();
radio_ip_addresses[i].Location = new Point(x, y);
radio_ip_addresses[i].Font = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);
radio_ip_addresses[i].ForeColor = Color.White;
radio_ip_addresses[i].TextAlign = ContentAlignment.MiddleLeft;
radio_ip_addresses[i].Visible = true;
radio_ip_addresses[i].Parent = this;
Thanks Rotem, i took your suggestion to check the designer.cs i should have thought of that. it was autosize that was the key :), see below from what i found in the designer.cs.
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(192, 50);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 71;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
Actually, I'm using C# with WPF. Let me explain again:
I have create UserControl (WPF) with one Button (Click on Button to create Image and textBox (Like New Folder)).
And I have another Window form (WPF) with Button and I want to Click on Button in Window Form to do Action instead of Button in UserControl. Here is the code of UserControl:
private void btnNewRoom_Click(object sender, RoutedEventArgs e)
{
Image img = new Image();
img.VerticalAlignment = System.Windows.VerticalAlignment.Top;
img.Width = 100;
img.Height = 100;
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var applicationDirectory = System.IO.Path.GetDirectoryName(assemblyLocation);
img.Source = new BitmapImage(new Uri("/Resources/Images/folder.png", UriKind.RelativeOrAbsolute));
StackPanel sp = new StackPanel();
TextBox tb = new TextBox();
tb.Width = 120;
tb.Height = 25;
tb.Text = "New Folder";
sp.Children.Add(img);
sp.Children.Add(tb);
AddNewRoom.Children.Add(sp);
}
and It is possible ?
Note: Any good way to Click button to Create Image and TextBox?
Implement a static event handler in your WPF control, and simply call it from your Window Form's button click event.
I have a custom control an inside of it I have a textbox that rotates depending wether you'd like it to collapse or expand, when it is collapsed I want the textbox to be vertical and when it is expanded I want it horizontal.
The problem is that when it is vertical the textbox doesn't show all the text, I've being looking for an answer, and I understand it has to do with the way silverlight updates it's layout. Here is my code
private void CollapseControl()
{
CollapseCommand.Content = "E";
CollapseCommand.Margin = _btnMarginOnCollapse;
BtnZoomIn.Visibility = Visibility.Collapsed;
BtnZoomOut.Visibility = Visibility.Collapsed;
ScrollViewerStackPanel.Visibility = Visibility.Collapsed;
ZoomPanel.Visibility = Visibility.Collapsed;
this.HorizontalAlignment = HorizontalAlignment.Left;
this.Width = 40;
RotateTransform nameRotateTransform = new RotateTransform();
nameRotateTransform.Angle = 270;
Nametb.RenderTransform = nameRotateTransform;
Nametb.VerticalAlignment = VerticalAlignment.Bottom;
Nametb.Height = Nametb.Width;
Nametb.Width = Nametb.Height;
Nametb.UpdateLayout();
}
One solution would be to use the LayoutTransformer control from the Silverlight toolkit.
You wrap the existing textblock inside a LayoutTransformer
<toolkit:LayoutTransformer x:Name="Namelt" ...>
<toolkit:LayoutTransformer.LayoutTransform>
<RotateTransform />
</toolkit:LayoutTransformer.LayoutTransform>
<TextBlock x:Name="Nametb" Text="Hello World" />
</toolkit:LayoutTransformer>
Then your code looks like:-
((RotateTransform)Namelt.LayoutTransform).Angle = 270;
Namelt.VerticalAlignment = VerticalAlignment.Bottom;
Namelt.Height = Nametb.Width;
Namelt.Width = Nametb.Height;
I just recently ran into a similar problem, and came up with the following solution (based on a post on the Silverlight forums), which should help with your issue, too:
private void CollapseControl()
{
CollapseCommand.Content = "E";
CollapseCommand.Margin = _btnMarginOnCollapse;
BtnZoomIn.Visibility = Visibility.Collapsed;
BtnZoomOut.Visibility = Visibility.Collapsed;
ScrollViewerStackPanel.Visibility = Visibility.Collapsed;
ZoomPanel.Visibility = Visibility.Collapsed;
this.HorizontalAlignment = HorizontalAlignment.Left;
LayoutTransform lt = new LayoutTransform();
lt.Content = Nametb;
RotateTransform nameRotateTransform = new RotateTransform();
nameRotateTransform.Angle = 270;
lt.LayoutTransform = nameRotateTransform;
lt.ApplyLayoutTransform();
Nametb.UpdateLayout();
}
I just written following and my similar problem is solved.
layoutTransform.VerticalAlignment = VerticalAlignment.Bottom;
layoutTransform.VerticalAlignment = VerticalAlignment.Center;