Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
405
Editing a xamgrid unboundcolumn using a IValueMultiConverter
posted

In my project I am using the xamgrid with some UnboundColumns.
In reading mode I have no issue. But I cannot ge the edit mode to work.
If I simply needed a IValueConverter I could get thigs to work following the sample provided but, for various reasons, I need to use a IMultiValueConverter instead.
This IMultiValueConverter need to take in as parameters the datacontect (UnboundDataContext) or the ColumnKey at a minimum.


For the EditorTemplate I started with the sample provided in your website below:
<DataTemplate x:Key="Sample">
<StackPanel Orientation="Vertical">
<Slider x:Name="slider1" SmallChange="1" LargeChange="1" DataContext="{Binding RowData}" Value="{Binding TestProperty, Converter={StaticResource subPropertyColumnConverter}, ConverterParameter={RelativeSource Self}}" />
<TextBlock Text="{Binding Value, ElementName=slider1}" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>

This compiles and "works" except that I would not enough information passed into the converter to do what I need

so I changed it to:

<DataTemplate x:Key="SampleTest">
<StackPanel Orientation="Vertical">
<Slider x:Name="slider1" SmallChange="1" LargeChange="1" DataContext="{Binding RowData}">
<Slider.Value>
<MultiBinding Converter="{StaticResource subPropertyColumnMultiConverter}">
<Binding Path="DataContext" RelativeSource="{RelativeSource Self}"/>
<Binding RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</Slider.Value>
</Slider>
<TextBlock Text="{Binding Value, ElementName=slider1}" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>


But I constantly crash.

Can you please provide a sample that shows how to edit an unboundcolumn using an IMultiValueParameter?

Thank you.

  • 6365
    Offline posted

    Hello,

    Thank you for the code-snippet you have provided.

    Based on the code you have provided, I presume you would like to pass the Slider control and the UnboundColumnDataContext as parameters for the Convert method of the multi converter. In this case, we can pass the slider's DataContext as the first binding object and the self referencing slider for the second binding object.

    <Slider.Value>
        <MultiBinding Converter="{StaticResource con}" Mode="TwoWay" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged">
            <!--================= Passing the UnboundColumnDataContext as first parameter. =================-->
            <Binding Path="DataContext" RelativeSource="{RelativeSource Self}" />

            <!--================= Passing the Slider as second parameter. =================-->
            <Binding RelativeSource="{RelativeSource Self}" Mode="OneWay"/>
        </MultiBinding>
    </Slider.Value>

    Since the implementation of the Convert and the ConvertBack methods is strongly dependent on the desired behavior and functionality, you can take a look at the sample methods below. Since we do not contain direct references to some elements in the converter, we will have to manually set some properties.

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var unboundColumnDataContext = values[0] as UnboundColumnDataContext;
        var slider = values[1] as Slider;

        if (unboundColumnDataContext == null || slider == null)
            return Binding.DoNothing;

        slider.Value = (unboundColumnDataContext.RowData as Item).Price;
        return Binding.DoNothing;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        double sliderValue = (double)value;

        var item = (Utilities.GetDescendantFromType(Application.Current.MainWindow, typeof(XamGrid), true) as XamGrid).ActiveItem as Item;
        item.Price = sliderValue;

        return null;
    }

    I have attached a sample application that demonstrates the approach from above. You can use it as a starting point for implementing the behavior you are looking for.

    If you have any questions, please let me know.

    XamGrid_sample.zip