How can I implement the following xaml binding in code behind?
<Canvas x:Name="_YAxis">
<Label Content="0.2" Canvas.Left="25" Canvas.Bottom="{Binding ElementName=_YAxis, Path=ActualHeight, Converter={StaticResource myPercentageOf}, ConverterParameter={StaticResource Constant_pt2} }" />
</Canvas>
Note, the converter simply multiplies the actual height of the Canvas by 0.2
I can sort out most kind of bindings, but this one has me stumped.
I can create the binding using
Label label = new Label() { label.Content = "0.2" };
Binding binding = new Binding("ActualHeight");
binding.Source = _YAxis;
// attach binding ???
_YAxis.Children.Add(label);
but how do I attach the binding to the Canvas.Left attached property?
Here you go:
Binding b = new Binding();
b.Path = new PropertyPath("ActualHeight");
b.Source = _YAxis;// OR b.ElementName = "_YAxis"
b.Converter = Resources["myPercentageOf"];
b.ConverterParameter = Resources["Constant_pt2"];
Label label = new Label() { label.Content = "0.2" };
_YAxis.Children.Add(label);
label.SetBinding(Canvas.BottomProperty, b); //Binding Canvas.Bottom to ActualHeight of _YAxis
Canvas.SetLeft(label, 25); //Setting Canvas.Left
Related
I have to say I am not getting used to WPF.
I have the problem that I have a Property that contains the value, but I want not to bind to that property.
I want to bind the property to a property named as the value in the property Name.
This is my xaml:
<telerik:RadMultiColumnComboBox x:Name="radMultiColumnComboBox_Part"
Grid.ColumnSpan="3" Grid.Row="1"
SelectedItem="{Binding SelectedItem, ElementName=selector, Mode=TwoWay}"
>
<telerik:RadMultiColumnComboBox.ItemsSourceProvider>
<telerik:GridViewItemsSourceProvider ItemsSource="{Binding ItemsSource, ElementName=selector}"
AutoGenerateColumns="False"
>
<telerik:GridViewItemsSourceProvider.Columns>
<!-- This line below is what I am talking about -->
<telerik:GridViewDataColumn DataMemberBinding="{Binding Name, ElementName=selector}" />
</telerik:GridViewItemsSourceProvider.Columns>
</telerik:GridViewItemsSourceProvider>
</telerik:RadMultiColumnComboBox.ItemsSourceProvider>
</telerik:RadMultiColumnComboBox>
I think mostly a try can explain it best:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var sourceProvider = radMultiColumnComboBox_Part.ItemsSourceProvider as GridViewItemsSourceProvider;
var columns = sourceProvider.Columns;
foreach(var gridViewColumn in sourceProvider.Columns)
{
var binding = new Binding(DisplayMemberPath);
binding.Source = gridViewColumn.DataContext;
// there is no property DataMemberBinding.
BindingOperations.SetBinding(gridViewColumn, Telerik.Windows.Controls.GridViewColumn.DataContextProperty, binding);
}
}
I would prefer a solution in xaml but in c# would also be fine.
Thank you.
This is not possible to do in pure XAML. You'll need to create the binding programmatically. Something like this:
var gridViewColumn = sourceProvider.Columns[0] as GridViewDataColumn;
string propertyToBindTo = selector.Name;
gridViewColumn.DataMemberBinding = new Binding(propertyToBindTo);
The solution:
var provider = this.GridViewItemsSourceProvider;
// var provider = this.radMultiColumnComboBox_Part.ItemsSourceProvider as GridViewItemsSourceProvider;
provider.Columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("Name") });
I need to have a datagrid column with some image, and for each cell I need to see, in the tooltip, the bigger image.
The datagrid is binded on a ObsevarbleCollection of MyOBjects, and MyImage and MyBigImage are poperties of MyObject.
With XAML here there is the working code:
<c1:C1DataGrid.Columns>
<c1:DataGridTemplateColumn>
<c1:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding MyImage}" >
<Image.ToolTip>
<ToolTip >
<StackPanel>
<Image Source="{Binding MyBigImage}" />
</StackPanel>
</ToolTip>
</Image.ToolTip>
</Image>
</DataTemplate>
</c1:DataGridTemplateColumn.CellTemplate>
</c1:DataGridTemplateColumn>
</c1:C1DataGrid.Columns>
But I need to move the code in the c# side.
So I've tried this code:
C1.WPF.DataGrid.DataGridTemplateColumn col1 = new
C1.WPF.DataGrid.DataGridTemplateColumn();
FrameworkElementFactory factoryImg = new
FrameworkElementFactory(typeof(Image));
Binding b1 = new Binding("MyImage");
b1.Mode = BindingMode.TwoWay;
factoryImg.SetValue(Image.SourceProperty, b1);
DataTemplate ttTemplate = new DataTemplate(typeof(StackPanel));
FrameworkElementFactory spFactory = new
FrameworkElementFactory(typeof(StackPanel));
spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory imgHolder = new
FrameworkElementFactory(typeof(Image));
Binding b2 = new Binding("MyBigImage");
b2.Mode = BindingMode.TwoWay;
imgHolder.SetBinding(Image.SourceProperty, b2);
spFactory.AppendChild(imgHolder);
ttTemplate.VisualTree = spFactory;
ToolTip tt = new System.Windows.Controls.ToolTip();
tt.ContentTemplate = ttTemplate;
factoryImg.SetValue(TextBlock.ToolTipProperty, tt);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factoryImg;
col1.CellTemplate = cellTemplate1;
MyDataGrid.Columns.Add(col1);
cellTemplate1.Seal();
With this code the image in the cell appears correctly, but the tooltip on the image is an empty panel, no image is visualized.
If I create a new BitmapImage the tooltip works, so I think it is a binding problem.
What I'm doing wrong?
Ok solved, I've forgot to set the DataContext property:
imgHolder.SetValue(TextBlock.DataContextProperty, List);
I need the following xaml code snippet in cs (code behind file)
<ToolBarTray Width="450" IsLocked="True" >
<ToolBar Width="{Binding ActualWidth,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ToolBarTray}}}">
<Button>B1</Button>
<Button>B2</Button>
</ToolBar>
</ToolBarTray>
If you really intend to use this code in the codebehind than the snippet below should be fine. However, it will not work if you want to create a DataTemplate from code. In that case you need to use FrameworkElementFactory derived types and not FrameoworkElement derived types.
public ToolBarTray CreatetoolBarTray()
{
var tbt = new ToolBarTray
{
Width = 450.0,
IsLocked = true
};
var tb = new ToolBar();
var b = new Binding
{
Path = new PropertyPath("ActualWidth"),
Source = new RelativeSource(RelativeSourceMode.FindAncestor, typeof (ToolBarTray), 1),
};
tb.SetBinding(WidthProperty, b);
tb.Items.Add(new Button() {Content = "b1"});
tb.Items.Add(new Button() {Content = "b2"});
tbt.ToolBars.Add(tb);
return tbt;
}
The control I created dynamically is a radiobutton, and I am trying to control the visibility of a hyperlinkbutton according to the IsChecked property of the radiobutton created in code-behind.
In my XAML file:
<HyperlinkButton Visibility="{Binding IsChecked, ElementName=tempRadio, Converter={StaticResource visibilityConvert}}" Content="Insert Record" Click="addRecord" Background="Aqua" Foreground="White"></HyperlinkButton>
Apparently I don't think I should use ElementName in this case, since it is only for controls created in XAML.
In my C# file:
public RadioButton tempRadio;
...
I would start with this:
first set the binding target on your hyperlink
hyperlinkButton.BindingTarget = tempRadio.IsChecked;
then set the binding:
hyperlinkButton.SetBinding(hyperlinkButton.BindingTarget, CreateValueBinding());
private Binding CreateValueBinding()
{
var valueBinding = new Binding();
valueBinding.Mode = BindingMode.TwoWay;
valueBinding.NotifyOnValidationError = true;
valueBinding.ValidatesOnExceptions = true;
valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path);
return valueBinding;
}
I want to add two columns in wpf datagrid one image & one text columns dynamically.
Xaml code :
<Grid><DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Margin="0" Name="grid" VerticalAlignment="Stretch" Width="Auto" ></DataGrid></Grid>
Code Behind:
DataGridTextColumn col = new DataGridTextColumn();
col.Header =Text1;
col.Binding =Text1;
grd.Columns.Add(col);
How do I add image column?or show image in the column?
Please suggest
Dee
As Anvaka said, you can Use DataGridTemplateColumn.
In C# you can add create DataGridTemplateColumn as this, Here i have added a CheckBox in to the DataGridTemplateColumn.
DataGridTemplateColumn col1 = new DataGridTemplateColumn();
col1.Header = "MyHeader";
FrameworkElementFactory factory1 = new FrameworkElementFactory(typeof(Image));
Binding b1 = new Binding("Picture");
b1.Mode = BindingMode.TwoWay;
factory1.SetValue(Image.SourceProperty, b1);
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = factory1;
col1.CellTemplate = cellTemplate1;
datagrid.Columns.Add(col1);
Here Picture is a property of ImageSource type in the class which collection is assigned to ItemsSource of DataGrid.
Use DataGridTemplateColumn. Define cell template in Window.Resources and use FindResource() to set column's CellTemplate property.
Hope this helps.
If you want to Set an Image in a DataGrid Column HEADER,
only programmatically, you can perform like this:
ImageSource image = new BitmapImage(new Uri(#"C:/téléchargement.jpg", UriKind.RelativeOrAbsolute));
Style style = new Style(typeof(DataGridColumnHeader));
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Image));
factory.SetValue(Image.SourceProperty, image);
factory.SetValue(Image.StretchProperty, Stretch.Uniform);
style.Setters.Add(new Setter { Property = TemplateProperty, Value = new ControlTemplate { TargetType = typeof(DataGridColumnHeader), VisualTree = factory } });
DataZone.Columns[5].HeaderStyle = style;
You can use this method for any type ( Ex : TextBlock , Label, ...), or create a more complex controlTemplate