Binding problem after selection on ListView  
Author Message
Cezary





PostPosted: Windows Presentation Foundation (WPF), Binding problem after selection on ListView Top

I have created a binding style1 which binds SelectedItem to a DependencyProperty and is programmatically applied to a control.

If a style is applied to a control before making selection the binding works, but if a selection is made first and style is applied after that the selection binding will not work...

Is that a bug.. or am I missing something (Try this code and see if you hit the breakpoint)

<Window x:Class="BorkenBindingAfterStyleSwitch.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="BorkenBindingAfterStyleSwitch" Height="395" Width="499"

Name="root">

<Grid>

<Button Height="31" HorizontalAlignment="Right" Margin="0,29,28,0" Name="button1" VerticalAlignment="Top" Width="129" Click="ShowStyle1">style1</Button>

<Button Height="31" HorizontalAlignment="Right" Margin="0,71,28,0" VerticalAlignment="Top" Width="129" Click="ShowStyle2">style2</Button>

<ListView Height="96" Margin="21,22,0,0" Name="listView1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120">

<TextBlock Text="string1"/>

<TextBlock Text="string2"/>

</ListView>

</Grid>

</Window>

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

public partial class Window1 : System.Windows.Window

{

public Window1()

{

InitializeComponent();

}

public static readonly DependencyProperty SelectionProperty =

DependencyProperty.Register("Selection",

typeof(object),

typeof(Window1),

new FrameworkPropertyMetadata(null, new PropertyChangedCallback(Callback)));

public object Selection

{

get { return (object)GetValue(SelectionProperty); }

set { SetValue(SelectionProperty, value); }

}

private static void Callback(DependencyObject obj, DependencyPropertyChangedEventArgs e)

{

// property changed

// SET BREAKPOINT HERE

}

private void ShowStyle1(object sender, RoutedEventArgs e)

{

this.listView1.Style = Application.Current.FindResource("style1") as Style;

}

private void ShowStyle2(object sender, RoutedEventArgs e)

{

this.listView1.Style = Application.Current.FindResource("style2") as Style;

}

}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

<Application x:Class="BorkenBindingAfterStyleSwitch.App"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

StartupUri="Window1.xaml"

>

<Application.Resources>

<Style x:Key="style1" TargetType ="{x:Type ListView}">

<Setter Property="SelectedItem" Value="{Binding Selection, ElementName=root}"/>

<Setter Property="Background" Value="green"/>

</Style>

<Style x:Key="style2" TargetType ="{x:Type ListView}">

<Setter Property="SelectedItem" Value="{x:Null}"/>

<Setter Property="Background" Value="yellow"/>

</Style>

</Application.Resources>

</Application>



Visual Studio 200819  
 
 
Douglas Stockwell





PostPosted: Windows Presentation Foundation (WPF), Binding problem after selection on ListView Top

I imagine this is because of Dependency Property Precedence - local values have higher priority than Style Setters: http://windowssdk.msdn.microsoft.com/en-us/library/ms743230.aspx

Does it work if you set the Binding on the ListView directly

- Doug


 
 
Cezary





PostPosted: Windows Presentation Foundation (WPF), Binding problem after selection on ListView Top

That's it... all I have to do is to ClearValue() before applying the template...

Thanks