Manually Updating GridViewColumn with DisplayMemberBinding binding set
-
Hi, I have a ListView with a GridView like this:
<GridView>
<GridViewColumn Header="Type" Width="Auto"
DisplayMemberBinding="{Binding Path=Type, Converter={StaticResource enumConverter}, Mode=OneWay}"/>
<GridViewColumn Header="Optimal" Width="60"
DisplayMemberBinding="{Binding Path=OptimalAttributeMass, Mode=OneWay, Converter={StaticResource doubleConverter}}"/>
<GridViewColumn Header="Nominal" Width="60"
DisplayMemberBinding="{Binding Path=NominalAttributeMass, Mode=OneWay, Converter={StaticResource doubleConverter}}"/>
<GridViewColumn Header="Control" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<Button DockPanel.Dock="Right" Content="Raise" Click="RaiseAttribute_Click"/>
<!--<TextBox/>-->
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>The only problematic column is:
<GridViewColumn Header="Nominal" Width="60"
DisplayMemberBinding="{Binding Path=NominalAttributeMass, Mode=OneWay, Converter={StaticResource doubleConverter}}"/>The NominalAttributeMass property is time dependent and always changes its value, so I can't implement INotifyPropertyChanged for it. Usually (when using a TextBlock) I just get the BindingExpression from the Control (with GetBindingExpression) and call UpdateTarget on it. But in this case there is no GetBindingExpression and the DisplayMemberBinding property is of the type BindingBase, where the function UpdateTarget doesn't exist. What is the proper way to Update the column?
-
Hi, I have a ListView with a GridView like this:
<GridView>
<GridViewColumn Header="Type" Width="Auto"
DisplayMemberBinding="{Binding Path=Type, Converter={StaticResource enumConverter}, Mode=OneWay}"/>
<GridViewColumn Header="Optimal" Width="60"
DisplayMemberBinding="{Binding Path=OptimalAttributeMass, Mode=OneWay, Converter={StaticResource doubleConverter}}"/>
<GridViewColumn Header="Nominal" Width="60"
DisplayMemberBinding="{Binding Path=NominalAttributeMass, Mode=OneWay, Converter={StaticResource doubleConverter}}"/>
<GridViewColumn Header="Control" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<Button DockPanel.Dock="Right" Content="Raise" Click="RaiseAttribute_Click"/>
<!--<TextBox/>-->
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>The only problematic column is:
<GridViewColumn Header="Nominal" Width="60"
DisplayMemberBinding="{Binding Path=NominalAttributeMass, Mode=OneWay, Converter={StaticResource doubleConverter}}"/>The NominalAttributeMass property is time dependent and always changes its value, so I can't implement INotifyPropertyChanged for it. Usually (when using a TextBlock) I just get the BindingExpression from the Control (with GetBindingExpression) and call UpdateTarget on it. But in this case there is no GetBindingExpression and the DisplayMemberBinding property is of the type BindingBase, where the function UpdateTarget doesn't exist. What is the proper way to Update the column?
One approach you can try is to define celltemplate for the column as shown below
<GridViewColumn x:Name="gdcol">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="txtbox" Text="{Binding Path}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>then in code where you want to update the target:
object obj = gdcol.CellTemplate.LoadContent();
TextBlock blk = obj as TextBlock;
BindingExpression exp = BindingOperations.GetBindingExpression(blk, TextBlock.TextProperty);
exp.UpdateTarget();I have not tried the code, you can give it a shot.