DependencyProperty绑定仅触发一次

时间:2015-05-05 11:17:03

标签: c# wpf binding dependency-properties

我有一个绑定到usercontrol上的依赖项属性的多绑定转换器。转换器似乎在初始化时被触发一次,但是当其多绑定项目引发更改通知时不会再次触发。

我不确定,但我怀疑问题可能是绑定被覆盖了。

发布我认为相关的所有代码。如果我遗漏任何东西,请告诉我。

XAML绑定到依赖属性**已编辑**:

<views:TextBlockComboBox.SelectedIndex>
            <MultiBinding Converter="{StaticResource UIDToIndexConverter}">
                <Binding Path="DataLayer.SupplierUID"></Binding>
                <Binding Path="CoreSuppliers"></Binding>
                <Binding Path="DataLayer" />
            </MultiBinding>
        </views:TextBlockComboBox.SelectedIndex>

UIDToLayerConverter类:

public class UIDToIndexConverter : IMultiValueConverter
    {

        public object Convert(
            object[] values,
            Type targetType,
            object parameter,
            System.Globalization.CultureInfo culture)
        {
            try
            {

                ILayer layer = (ILayer)values[0];

                long? UID = (long?)values[0];

                ObservableCollection<IMaterialList> materialLists = (ObservableCollection<IMaterialList>)values[1];

                foreach (IMaterialList materialList in materialLists)
                {
                    if (materialList.UID.Equals(UID))
                    {
                        return materialLists.IndexOf(materialList);
                    }
                }

                return 0;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public object[] ConvertBack(
            object value,
            Type[] targetTypes,
            object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

TextBlockComboBox中设置的依赖属性

public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register(
            "SelectedIndex",
            typeof(int),
            typeof(TextBlockComboBox),
            new FrameworkPropertyMetadata(SelectedIndexPropertyChangedCallback)


public int SelectedIndex
        {
            get
            {
                return (int)GetValue(SelectedIndexProperty);
            }
            set
            {
                SetCurrentValue(SelectedIndexProperty, value);
            }
        }

private static void SelectedIndexPropertyChangedCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args)
        {
        }
            );

编辑工作绑定

<views:TextBlockComboBox.Visibility>
                <MultiBinding Converter="{StaticResource LayerEditConverter}">
                    <Binding Path="DataLayer" />
                    <Binding>
                        <Binding.Source>
                            <system:String>Core</system:String>
                        </Binding.Source>
                    </Binding>

                </MultiBinding>
            </views:TextBlockComboBox.Visibility>

编辑我发现如果我将usercontrol上的绑定删除到selectedindex依赖项属性,则转换器将触发。

<ComboBox 
            x:Name="ThisComboBox"
            ItemsSource="{Binding Items, ElementName=ThisUserControl, Mode=TwoWay}" 
            SelectedItem="{Binding SelectedItem, ElementName=ThisUserControl, Mode=TwoWay}"
            SelectedIndex="{Binding SelectedIndex, ElementName=ThisUserControl, Mode=TwoWay}" --WITHOUT THIS IN CONVERTER FIRES__
            SelectionChanged="selectedItemChanged"
            VerticalAlignment="Center"
            DisplayMemberPath="{Binding DisplayPath, ElementName=ThisUserControl, Mode=TwoWay}" 
         />

0 个答案:

没有答案
相关问题