Reputation: 21
I have been working on this for a while. Still can't seem to get it. I am trying to have a Setter Property bound to a value in my ObservableCollection populate from my DB. I have learned that Style Setters cannot be bound in Xaml. I have read and used a good example UWP Binding in Style Setter not working. But I still can't get my UI to refect the changes. My Model in Item.cs:
namespace MyTest
{
class Item
{
public int Id { get; set; }
public string Name { get; set; }
public bool MyIsVisible { get; set; }
public string MyVisibility { get; set; }
}
My MainPage.Xaml.CS:
using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace MyTest
{
public sealed partial class MainPage : Page
{
ObservableCollection<Item>Items = new ObservableCollection<Item>();
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Items.Add(new Item() { Id = 1, Name = "Item A", MyIsVisible=true, MyVisibility = "Visible" });
Items.Add(new Item() { Id = 2, Name = "Item B", MyIsVisible=false, MyVisibility = "Collapsed" });
Items.Add(new Item() { Id = 3, Name = "Item C", MyIsVisible=true, MyVisibility = "Visible" });
Items.Add(new Item() { Id = 4, Name = "Item D", MyIsVisible=true, MyVisibility = "Visible" });
MyList.ItemsSource = Items;
}
}
}
My MainPage.Xaml:
<Page
x:Class="MyTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<DataTemplate x:Name="lvDataTemplate" x:DataType="local:Item">
<TextBlock Text="{x:Bind Path=Name}"/>
</DataTemplate>
<Style x:Key="lvItemStyle" TargetType="ListViewItem">
<Setter Property="local:BindingHelper.ListViewItemVisibilityBindingPath" Value="MyVisibility"/>
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Grid.Row="1" Name="MyList" Margin="12, 0, 12, 0"
ItemTemplate="{StaticResource lvDataTemplate}"
ItemContainerStyle="{StaticResource lvItemStyle}" />
</Grid>
</Page>
And lastly the BindingHelper.cs:
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace MyTest
{
public class BindingHelper
{
public static readonly DependencyProperty ListViewItemVisibilityBindingPathProperty =
DependencyProperty.RegisterAttached(
"ListViewItemVisibilityBindingPath", typeof(string), typeof(BindingHelper),
new PropertyMetadata(null, ListViewItemBindingPathPropertyChanged));
public static string GetListViewItemVisibilityBindingPath(DependencyObject obj)
{
return (string)obj.GetValue(ListViewItemVisibilityBindingPathProperty);
}
public static void SetListViewItemVisibilityBindingPath(DependencyObject obj, string value)
{
obj.SetValue(ListViewItemVisibilityBindingPathProperty, value);
}
private static void ListViewItemBindingPathPropertyChanged(
DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var propertyPath = e.NewValue as string;
if (propertyPath != null)
{
var gridProperty = ListViewItem.VisibilityProperty;
BindingOperations.SetBinding( obj, gridProperty,
new Binding { Path = new PropertyPath(propertyPath) });
}
}
}
}
I have tried using the DB MyVisibilty (string) as well as MyIsVisible (bool) and still not able to Collapse the Visibility of Item B. I am getting no errors. It seems like the BindingHelper.ListViewItemVisibilityBindingPath is still not getting the DB value "MyVisiblity".
Anybody know what I am missing?
Edit 3/14/16 - After working on this extensively. I have found what appears to be the issue. I changed the ListView to a ListBox and the code worked! There is apparently an issue/limitation to the ListView which will not allow it's Setter Properties to be set by using the Dependency Binding Helper. Can anybody think of a solution or workaround for this???
Upvotes: 0
Views: 554
Reputation: 128061
Change the type of the MyVisibility
property from string
to Visibility
:
class Item
{
...
public Visibility MyVisibility { get; set; }
}
Upvotes: 1