locked
Modifying a ConverterParameter Programmatically

    Question

  • I have the following in my Page.Resources:
    <x:Int32 x:Key="SelectedGiftIndex">0</x:Int32>
    And the following in my page:
    <Pivot x:Name="pivRecipients" Grid.Row="1" Background="Transparent">
    	<Pivot.ItemContainerStyle><Style TargetType="PivotItem"><Setter Property="Margin" Value="0"/></Style></Pivot.ItemContainerStyle>
    	<Pivot.HeaderTemplate><DataTemplate><TextBlock Text="{Binding Name}" FontSize="60" FontFamily="Arial"/></DataTemplate></Pivot.HeaderTemplate>
    	<Pivot.ItemTemplate>
    		<DataTemplate>
    			<ScrollViewer>
    				<ItemsControl ItemsSource="{Binding Gifts,Converter={StaticResource GiftsWithIndex}}">
    					<ItemsControl.ItemsPanel><ItemsPanelTemplate><StackPanel/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
    					<ItemsControl.ItemTemplate><DataTemplate><RadioButton Style="{StaticResource GiftRadioButtonStyle}" Content="{Binding}" Checked="Gift_Checked" Tag="{Binding Index}" IsChecked="{Binding Index,Converter={StaticResource BooleanEquals},ConverterParameter={StaticResource SelectedGiftIndex}}"/></DataTemplate></ItemsControl.ItemTemplate>
    				</ItemsControl>
    			</ScrollViewer>
    		</DataTemplate>
    	</Pivot.ItemTemplate>
    </Pivot>
    As you can see, I use the resource (an Int32/Integer) as the ConverterParameter in my ItemsControl's DataTemplate's RadioButton. This works fine, except for the fact that I cannot change the resource programmatically. I tried the following:
    Me.Resources("SelectedGiftIndex") = 1
    I even checked to make sure it (I thought) got changed by using the following code:
    System.Diagnostics.Debug.WriteLine("SelectedGiftIndex: {0}", Me.Resources("SelectedGiftIndex").ToString())
    And according to this, it did get changed. But the rest of my code still sees it as it's original value (0). I have read that resources cannot be changed because they are compiled with the rest of the code. That would explain why all this is happening. So why do I want to use a resource for this? Because the ConverterParameter in my ItemsControl's DataTemplate's RadioButton is not constant, I need a way to change it programmatically, and I was not sure how else to do that. I have never programmatically modified a Binding before, and I figured it would be more efficient (if it was an option, but I guess it isn't) to modify a single value (the resource) than recreate the Binding every time. I have heard stuff about binding to variables, but I have never figured out how to bind to anything defined in the codebehind. Can anybody help me find a way to programmatically edit the ConverterParameter? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, January 13, 2015 2:31 AM

Answers

  • There are probably other ways to do this, but let's do something easy... create a hidden element (like a TextBlock with visibility set to collapsed) in your XAML page and bind to that.   You can change it to whatever you want in the code behind and binding works perfectly. 

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, January 13, 2015 2:51 PM
    Moderator

All replies

  • There are probably other ways to do this, but let's do something easy... create a hidden element (like a TextBlock with visibility set to collapsed) in your XAML page and bind to that.   You can change it to whatever you want in the code behind and binding works perfectly. 

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, January 13, 2015 2:51 PM
    Moderator
  • That is a good workaround! Like you said, there are probably other ways to do it, but since I don't know them and yours is easy and works, I'm takin' it! Thanks!

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, January 13, 2015 4:20 PM
  • I am having trouble accessing the hidden element. Here is the element I have added:
    <TextBlock x:Name="txtSelectedGiftIndex" Visibility="Collapsed" Width="0"/>
    And here is my new binding code:
    <ItemsControl ItemsSource="{Binding Gifts,Converter={StaticResource GiftsWithIndex}}" FontFamily="Global User Interface">
    	<ItemsControl.ItemsPanel><ItemsPanelTemplate><StackPanel/></ItemsPanelTemplate></ItemsControl.ItemsPanel>
    	<ItemsControl.ItemTemplate><DataTemplate><RadioButton Style="{StaticResource GiftRadioButtonStyle}" Content="{Binding}" Checked="Gift_Checked" Tag="{Binding Index}" IsChecked="{Binding Index,Converter={StaticResource BooleanEquals},ConverterParameter={Binding Width,ElementName=txtSelectedGiftIndex,Converter={StaticResource ToInteger}}}"/></DataTemplate></ItemsControl.ItemTemplate>
    </ItemsControl>
    However, this gives me a NullReferenceException for the parameter argument in my BooleanEquals converter (an IValueConverter that returns True or False based on the value and parameter). I have tested that the binding expression I use for the ConverterParameter works:
    {Binding Width,ElementName=txtSelectedGiftIndex,Converter={StaticResource ToInteger}}
    By using it for the Text property of another TextBlock. So I am assuming that the problem has something to do with the fact that the binding expression is inside the DataTemplate. What do I need to do to access an external element from inside a DataTemplate? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, January 13, 2015 10:45 PM