Result in Installed app differ from result during debug - c#

I have a program where in I add column with buttons programmatically. When I'm in the visual studio it work accordingly but when I install it, The app crashed. When I remove the background image of the button it do work even in the installed app. here is my code in how I add a column with button in my datagrid.
///Here is the part where I declare the path of my image background
Image EditImage = new Image();
EditImage.Source = new BitmapImage(new Uri("../../Icons/pencil.png", UriKind.Relative));
ImageBrush imgEditBrush = new ImageBrush(EditImage.Source);
//Here is the part where I add my column
DataGridTemplateColumn Edit = new DataGridTemplateColumn();
Edit.Header = "Edit";
FrameworkElementFactory EditFactory = new FrameworkElementFactory(typeof(Button));
Binding b1 = new Binding("IsClicked");
b1.Mode = BindingMode.TwoWay;
EditFactory.SetValue(Button.BackgroundProperty, imgEditBrush);
EditFactory.SetValue(Button.WidthProperty, size);
EditFactory.AddHandler(Button.ClickEvent, new RoutedEventHandler(EditTransaction_Click));
DataTemplate CellEdit = new DataTemplate();
CellEdit.VisualTree = EditFactory;
Edit.CellTemplate = CellEdit;
dgDataGrid.Columns.Add(Edit);
What's wrong with my declaration of Button.BackgroundProperty? Can anyone help me out. Thank you in advance.

Related

Gtk# application is not rendering TreeView Properly

I am trying to develop a gtk application on mac using gtk#. I created a tree view just like in the tutorial but the arrows are not rendering properly as seem below, there are no arrows. I can click to open and close but the actual arrows aren't working.
I copy pasted the code from the tutorial and It did the same thing, but here is my code anywhere. I have also verified view.ShowExpanders is set to true
Application.Init();
Window window = new Window("Editor Window");
window.SetPosition(WindowPosition.Center);
window.HeightRequest = 800;
window.WidthRequest = 1200;
TreeView view = new TreeView();
view.WidthRequest = 500;
TreeViewColumn column = new TreeViewColumn();
column.Title = "Heirarchy";
CellRendererText cell = new CellRendererText();
column.PackStart(cell, true);
view.AppendColumn(column);
column.AddAttribute(cell, "text", 0);
TreeStore store = new TreeStore(typeof(string));
TreeIter i = store.AppendValues("Project");
i = store.AppendValues(i,"Room1");
i = store.AppendValues(i, "Item");
store.AppendValues(i, "Attribute");
store.AppendValues(i, "Attribute");
store.AppendValues(i, "Attribute");
view.ShowExpanders = true;
view.Model = store;
view.ShowExpanders = true;
window.Add(view);
window.DeleteEvent += ExitWindow;
window.ShowAll();
Application.Run();
Is there some sort of image asset that doesn't exist on my computer? How would I install that on mac? Thank you for any help you can provide.
As a comment suggested, using brew install adwaita-icon-theme fixed the problem

WPF CheckBox checked event on scrolling thought datagrid

I have a datagrid with a DataGridTemplateColumn containing a CheckBox. I create the column like this:
DataGridTemplateColumn cTemplateColumn = new DataGridTemplateColumn();
cTemplateColumn.Header = "Auswahl";
FrameworkElementFactory cFactory = new FrameworkElementFactory(typeof(CheckBox));
Binding b1 = new Binding("[__intern_cv__]");
//b1.IsAsync = true;
b1.Converter = new StringToBoolConverter(this);
b1.Mode = BindingMode.TwoWay;
b1.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
cFactory.SetValue(CheckBox.IsCheckedProperty, b1);
cFactory.SetValue(CheckBox.HorizontalAlignmentProperty, HorizontalAlignment.Center);
cFactory.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(CheckedEvent));
cFactory.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(CheckedEvent));
cFactory.AddHandler(CheckBox.PreviewMouseDownEvent, new MouseButtonEventHandler(checkBoxMouseDown));
DataTemplate cCellTemplate = new DataTemplate();
cCellTemplate.VisualTree = cFactory;
cTemplateColumn.CellTemplate = cCellTemplate;
Columns.Add(cTemplateColumn);
Now i have the problem, that during scrolling throught the DataGrid, the CheckedEvent is called, with Checked == False.
The vents a subscripted here:
cFactory.AddHandler(CheckBox.CheckedEvent, new RoutedEventHandler(CheckedEvent));
cFactory.AddHandler(CheckBox.UncheckedEvent, new RoutedEventHandler(CheckedEvent));
How can scrolling call the event?
maybe someone has an idea, thank you!
By default the DataGrid virtualizes its rows. This means that during scrolling old rows are re-used with new data. If the new data has "[__intern_cv__]" set to false, while the previous data had it set to true an Unchecked event will be raised.

How to get a WriteableBitmap image from an un-rendered control (UIElemet) in Silverlight?

I've been able to create a WriteableBitmap from controls that are rendered in my Silverlight application UI and export them to an XImage via SilverPDF and include them in a PDF document. This conversion and export process works fine.
Now, I'm working on a piece where I have a DataGrid that I'm building completely in the code behind and I do not intend to render it on the UI. My problem now is getting a WriteableBitmap of the DataGrid so that I can run it through the above process to convert it to an XImage and export it to a PDF document.
I've seen this thread which seems to also describe my situation, but it did not help: WPF - Get size of UIElement in Memory?
I'm wondering if the Measure and Arrange functions only apply on the WPF side and not Silverlight.
How can I get a WriteableBitmap from an un-rendered control? Here is the relevant code so far:
//Add DataGrid with feature's attributes.
DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = false;
dg.HeadersVisibility = DataGridHeadersVisibility.None;
DataGridTextColumn keyColumn = new DataGridTextColumn
{
Binding = new Binding {Path = new PropertyPath("Key")}
};
DataGridTextColumn valueColumn = new DataGridTextColumn
{
Binding = new Binding {Path = new PropertyPath("Value")}
};
dg.Columns.Add(keyColumn);
dg.Columns.Add(valueColumn);
dg.ItemsSource = _featureToPrint.Attributes;
sp.Children.Add(dg);
return sp;
}
Once that sp (stackpanel) is returned, I call the Measure / Arrange method on it:
private static void ArrangeElement(UIElement element, double height, double width)
{
var box = new Viewbox { Child = element };
box.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
box.Arrange(new Rect(10, 10, width, height));
}
Any help or explanation of what I'm missing would be greatly appreciated! Thanks!

Change button background

After several attempts to change the background of a button, and getting errors of casting and things like that I finnaly got to this point:
Uri dir = new Uri("red_flag.png",UriKind.RelativeOrAbsolute);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
ImageBrush bru = new ImageBrush();
bru.ImageSource=source;
bru.Opacity = 100;
This code does not generate errors, but I can't see the changes when I call:
button1.background = bru;
It just makes anything! :(
found the answer myelf after reading Mick's answer, I share with you what I did:
Uri dir = new Uri("red_flag.png", UriKind.Relative);
ImageSource source = new System.Windows.Media.Imaging.BitmapImage(dir);
Image image = new Image();
image.Source = source;
StackPanel stack = new StackPanel();
stack.Children.Add(image);
myButton.Content = stack;
Thanks for your help
Update 1:
For best results set the padding property of your button to 0 (in each of the cases) so the image can resize automatically to fill all the button, please note this could hide your actual content, in my case this was what I wanted.
If this code is part of the click event handler for the same button you will have this problem.
Peter Torr explains why here and offers a solution.
Why can't I change the Background of my Button on a Click event?

How do I build a DataTemplate in c# code?

I am trying to build a dropdown list for a winform interop, and I am creating the dropdown in code. However, I have a problem getting the data to bind based on the DataTemplate I specify.
What am I missing?
drpCreditCardNumberWpf = new ComboBox();
DataTemplate cardLayout = new DataTemplate {DataType = typeof (CreditCardPayment)};
StackPanel sp = new StackPanel
{
Orientation = System.Windows.Controls.Orientation.Vertical
};
TextBlock cardHolder = new TextBlock {ToolTip = "Card Holder Name"};
cardHolder.SetBinding(TextBlock.TextProperty, "BillToName");
sp.Children.Add(cardHolder);
TextBlock cardNumber = new TextBlock {ToolTip = "Credit Card Number"};
cardNumber.SetBinding(TextBlock.TextProperty, "SafeNumber");
sp.Children.Add(cardNumber);
TextBlock notes = new TextBlock {ToolTip = "Notes"};
notes.SetBinding(TextBlock.TextProperty, "Notes");
sp.Children.Add(notes);
cardLayout.Resources.Add(sp, null);
drpCreditCardNumberWpf.ItemTemplate = cardLayout;
Assuming that you've already set up the ItemsSource etc for drpCreditCardNumberWpf...
//create the data template
DataTemplate cardLayout = new DataTemplate();
cardLayout.DataType = typeof(CreditCardPayment);
//set up the stack panel
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
spFactory.Name = "myComboFactory";
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
//set up the card holder textblock
FrameworkElementFactory cardHolder = new FrameworkElementFactory(typeof(TextBlock));
cardHolder.SetBinding(TextBlock.TextProperty, new Binding("BillToName"));
cardHolder.SetValue(TextBlock.ToolTipProperty, "Card Holder Name");
spFactory.AppendChild(cardHolder);
//set up the card number textblock
FrameworkElementFactory cardNumber = new FrameworkElementFactory(typeof(TextBlock));
cardNumber.SetBinding(TextBlock.TextProperty, new Binding("SafeNumber"));
cardNumber.SetValue(TextBlock.ToolTipProperty, "Credit Card Number");
spFactory.AppendChild(cardNumber);
//set up the notes textblock
FrameworkElementFactory notes = new FrameworkElementFactory(typeof(TextBlock));
notes.SetBinding(TextBlock.TextProperty, new Binding("Notes"));
notes.SetValue(TextBlock.ToolTipProperty, "Notes");
spFactory.AppendChild(notes);
//set the visual tree of the data template
cardLayout.VisualTree = spFactory;
//set the item template to be our shiny new data template
drpCreditCardNumberWpf.ItemTemplate = cardLayout;
You can use the same way I have set the ToolTip on the TextBlocks to set other properties such as margins.
The full version
var ms = new MemoryStream(Encoding.UTF8.GetBytes(#"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
xmlns:c=""clr-namespace:MyApp.Converters;assembly=MyApp"">
<DataTemplate.Resources>
<c:MyConverter x:Key=""MyConverter""/>
</DataTemplate.Resources>
<TextBlock Text=""{Binding ., Converter={StaticResource MyConverter}}""/>
</DataTemplate>"));
var template = (DataTemplate)XamlReader.Load(ms);
var cb = new ComboBox { };
//Set the data template
cb.ItemTemplate = template;
Well, indeed we still have another way, you will really like it if you dislike those FrameworkElementFactory things.
And I think it just makes minor changes to the natural code, that is, declare a UserControl and put your control into it, and then, use just one FrameworkElementFactory to call the UserControl.
Simple demo code (in F#):
let buildView()=StackPanel()
//Build it with natural code
type MyView()=inherit UserControl(Content=buildView())
let factory=FrameworkElementFactory(typeof<MyView>)
let template=DataTemplate(VisualTree=factory)
let list=ItemsControl(ItemsSource=makeData(),ItemTemplate=template)

Categories