I have the following wpf control added to xaml:
<ListView Margin="22,80,271,12" Name="listView1" ItemsSource="{Binding}" />
I know how to create a ListView object programmatically. The only thing that I am missing is how could I add the property
ItemsSource="{Binding}"
with code to that object. I have already managed to add the columns and gridview with c#. The only thing that I am missing is to add that property ItemsSource="{Binding}"
I have tried looking for an answer here.
Shortest should be this (literal translation of XAML):
listView1.SetBinding(ListView.ItemsSourceProperty, new Binding());
listView1.ItemsSource = listView1.DataContext as IEnumerable;
Is this what you are looking for?
Binding myBinding = new Binding();
myBinding.ElementName = "item-you-are-binding-to";
myBinding.Path = new System.Windows.PropertyPath("property-you-are-binding-to");
listView1.SetBinding(ContentProperty, myBinding);
All you need to do is this:
var binding = new Binding("DataContext");
binding.Source = listView1;
listView1.SetBinding(ListView.ItemsSourceProperty, binding);
Related
For some reason I can't use any binding with a UWP Xaml SwipeItem Control. I've tried it in many different ways and with different SwipeItem properties but every time it is null. What's even stranger is any type of x:Bind to any property and it will crash.
if anyone marks this:
SwipeItem XAML Binding ignored
as a duplicate question it isn't so don't do it or I'll freak out. That question wasn't even answered.
<SwipeControl>
<SwipeControl.LeftItems>
<SwipeItems Mode="Execute">
<SwipeItem Text="{Binding}" Background="{StaticResource MIIGreen}" BehaviorOnInvoked="Close"/>
</SwipeItems>
</SwipeControl.LeftItems>
<SwipeControl.RightItems>
<SwipeItems Mode="Execute">
<SwipeItem Background="{StaticResource MIIRed}" BehaviorOnInvoked="Close" Command="{StaticResource CommandEnclosureNotInstalled}" CommandParameter="{Binding}"/>
</SwipeItems>
</SwipeControl.RightItems>
</SwipeControl>
the DataContext is just a simple DataModel and all other controls are binding fine. the command is from a staticresource and the command is firing just fine. in this example any combination of Binding or x:Bind either does nothing or crashes when trying to bind ANYTHING to Text or CommandParamter properties. There has to be something wrong with SwipItem controls, I need a way to pass the DataContext through the CommandParameter.
SwipeControl is not a standard itemControl, it does not have a dataTemplate, so SwipeItem can't find the DataContext of the parent view, so you can't directly use Binding directly in xaml. It seems you can only use Binding in code.(Below I give example of LeftItems).
in xaml:
<SwipeControl Margin="50" LeftItems="{Binding leftItems}">
</SwipeControl>
in cs:
public SwipeItems leftItems { get; set; }
public MainPage()
{
this.InitializeComponent();
SwipeItem leftItem = new SwipeItem();
Binding myBinding = new Binding();
myBinding.Source = viewmodel;
myBinding.Path = new PropertyPath("MyText"); //the property in viewmodel
BindingOperations.SetBinding(leftItem, SwipeItem.CommandParameterProperty, myBinding);
BindingOperations.SetBinding(leftItem, SwipeItem.TextProperty, myBinding);
Binding commandBinding = new Binding();
commandBinding.Source = new FavoriteCommand(); //command class
BindingOperations.SetBinding(leftItem, SwipeItem.CommandProperty, commandBinding);
leftItems = new SwipeItems() {
leftItem
};
this.DataContext = this;
}
I've got an issue where if an item is selected in a list I want it to update my items in my grid. The binding is done by:
<ScrollViewer Grid.Row="1">
<ItemsControl x:Name="RightGridItemsControl" ItemsSource="{Binding News}" ItemTemplate="{StaticResource RightGridTemplate}"/>
</ScrollViewer>
When an item, e.g. Planet is selected, I want to update the ItemsSource binding to a new list. This is specified in my DataModel.
How can I update this programmatically? I've tried something like this, but it requires a DependencyObject and can't find out what it means. This also looks like WPF rather than UWP.
`var myBinding = new Binding
{
Source = Planets,
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
BindingOperations.SetBinding(new , ItemsControl.ItemsSourceProperty, myBinding);`
What should I put as the first item for the contstructor for 'SetBinding'?
You can set the Binding like this:
BindingOperations.SetBinding(
RightGridItemsControl, ItemsControl.ItemsSourceProperty, myBinding);
or like this:
RightGridItemsControl.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
Note also that currently there is no property path present in your Binding. If there is a News property as in your XAML, the Binding should probably look like shown below, without Mode = BindingMode.OneWay, which is the default, and without UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, which has no effect in a one-way binding.
var myBinding = new Binding
{
Source = Planets,
Path = new PropertyPath("News")
};
How can I bind width in C#?
In XAML it would be
Width="{Binding Path=ActualWidth, ElementName=img}"
I need to bind it in C# to update the width
Binding mybinding = new Binding();
mybinding.Path.Path = "ActualWidth";
mybinding.ElementName = "img"
Mytarget.SetBinding(MYTARGET.WidthProperty, mybinding);
MYTARGET is class and Mytarget is object name.
I made it work like this
Binding b = new Binding();
b.Source = img;
b.Path = new PropertyPath("ActualWidth");
ui.SetBinding(Grid.WidthProperty, b);
if anyone is interested, setting it as Path.Path doesn't work as intended, but this does.
I have 2 listboxes populated by data like so:
GoogleNewsResults.Add(
new News()
{
Content = "News Content", Title = "News Title"
});
NotifyPropertyChanged("GoogleNewsResults");
GoogleBlogsResults.Add(
new Blog()
{
Content = "Blog Content", Title = "Blog Title"
});
NotifyPropertyChanged("GoogleBlogsResults");
But it doesnt update Blog Results listbox. Do you have any idea why?
The XAML has this type of binding:
<sllb:ListBox x:Name="GoogleBlogsList" ItemsSource="{Binding GoogleBlogsResults, Mode=TwoWay}" />
As your GoogleBlogsResults property is a
List<Blog>
adding items to it will not trigger the binding engine to fire as the object reference has not changed when you call
NotifyPropertyChanged("GoogleBlogsResults");
You can solve this by following the solution described by T.Ho by using
ObservableCollection<Blog>
which triggers the binding engine automatically when items within the collection are modified or alternatively by generating a new
List<Blog>
object (which merges the new and old items) and setting the GoogleBlogsResults property to the new list.
Hope this helps.
this Tutorial may help you
i think you need to binding source in XAML
DataContext="{Binding Source={StaticResource CustomerContainerObject}}"
or
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
var employees = new List<Employee>()
{
new Employee { Name = "Scott" },
new Employee { Name = "Poonam"},
new Employee { Name = "Paul" }
};
this.DataContext = employees;
wish that helps.
Instead of implement INotifyProperityChanged interface yourself, you might want to use ObservableCollection<T> instead for your list items. ObservableCollection is a collection class with INotifyProperityChanged implemented, so you don't have to manually call "NotifyPropertyChanged" for the binding to update.
Make sure you have the right DataContext. If you're not using MVVM design pattern, you might want to look into that.
Can anyone tell me what is the c# equivalent of the following snippet of XAML ??
<my:DataGridTextColumn
Visibility="{Binding Path=DataColumns[21].IsVisible, Source={StaticResource viewmodel}, Converter={StaticResource vc}}"
Binding="{Binding SdDevDuration}"
/>
Its the visibility binding I cannot get right. DataGridTextColumn is not a FrameworkElement so no SetBinding method.
Thanks in advance,
Matt
I worked this out. For anyone who's interested you can use the BindingOperation.SetBinding method.
The full code is,
var newCol = new DataGridTextColumn();
newCol.Binding = new Binding("SdDevDuration");
var visiblityBinding = new Binding("IsVisible");
visiblityBinding.Source = col;
visiblityBinding.Converter = new VisibilityConverter();
BindingOperations.SetBinding(newCol, DataGridTextColumn.VisibilityProperty, visiblityBinding);