嵌入到另一个控件中时,ControlTemplate无法正常工作

时间:2018-04-11 08:20:53

标签: c# wpf controltemplate

因为现有的ComboBox控件在标记枚举方面并不满足我的期望,所以我为它创建了一个自定义控件。

基本上我只创建了一个下拉按钮的样式和一个继承自提供数据源的下拉按钮的类。

这是我创作的风格:

<Style TargetType="dx:DropDownButton" x:Key="FlagEnumEditStyle">
    <Style.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibility" />
    </Style.Resources>
    <Setter Property="Content">
        <Setter.Value>
            <TextBlock Text="{Binding ActualContent, Mode=OneWay}" TextWrapping="Wrap" />
        </Setter.Value>
    </Setter>
    <Setter Property="PopupContent">
        <Setter.Value>
            <Border BorderThickness="1" BorderBrush="Black" Background="White">
                <ScrollViewer Margin="5,5,0,5" VerticalScrollBarVisibility="Auto" Padding="0,0,5,0">
                    <StackPanel Orientation="Vertical">
                        <CheckBox Content="None" Visibility="{Binding HasNoneValue, Converter={StaticResource BoolToVisibility}}" IsChecked="{Binding NoneChecked}" />
                        <CheckBox Content="All" Visibility="{Binding HasAllValue, Converter={StaticResource BoolToVisibility}}" IsChecked="{Binding AllChecked}" />
                        <Separator HorizontalAlignment="Stretch" Visibility="{Binding IsFlagEnum, Converter={StaticResource BoolToVisibility}}" />
                        <ItemsControl ItemsSource="{Binding Items}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" Visibility="{Binding Visibility}" />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </ScrollViewer>
            </Border>
        </Setter.Value>
    </Setter>
</Style>

当我创建一个&#34; FlagEnumEdit&#34; control(即继承自DropDownButton的类)并将其设置为指定的样式,然后它的效果非常好。

现在我获得了 DevExpress Grid ,它允许我通过指定ControlTemplate并将其目标绑定到EditValue来创建自定义单元格编辑器(请参阅他们的documentation)。

但是当我这样做时,我在样式中的所有绑定上都会出错:

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=(0).TextDecorations; DataItem='EditGridCellData' (HashCode=14515408); target element is 'TextBox' (Name=''); target property is 'TextDecorations' (type 'TextDecorationCollection')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=(0).TextDecorations; DataItem='EditGridCellData' (HashCode=14515408); target element is 'TextBox' (Name=''); target property is 'TextDecorations' (type 'TextDecorationCollection')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=(0).TextDecorations; DataItem='EditGridCellData' (HashCode=14515408); target element is 'TextBox' (Name=''); target property is 'TextDecorations' (type 'TextDecorationCollection')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ActualContent; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HasNoneValue; DataItem=null; target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=NoneChecked; DataItem=null; target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HasAllValue; DataItem=null; target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=AllChecked; DataItem=null; target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=IsFlagEnum; DataItem=null; target element is 'Separator' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Items; DataItem=null; target element is 'ItemsControl' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Error: 40 : BindingExpression path error: 'HasNoneValue' property not found on 'object' ''FlagEnumEdit' (Name='PART_Editor')'. BindingExpression:Path=HasNoneValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=HasNoneValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=HasNoneValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HasNoneValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Error: 40 : BindingExpression path error: 'HasAllValue' property not found on 'object' ''FlagEnumEdit' (Name='PART_Editor')'. BindingExpression:Path=HasAllValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=HasAllValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=HasAllValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HasAllValue; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'CheckBox' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Error: 40 : BindingExpression path error: 'IsFlagEnum' property not found on 'object' ''FlagEnumEdit' (Name='PART_Editor')'. BindingExpression:Path=IsFlagEnum; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'Separator' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=IsFlagEnum; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'Separator' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=IsFlagEnum; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'Separator' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=IsFlagEnum; DataItem='FlagEnumEdit' (Name='PART_Editor'); target element is 'Separator' (Name=''); target property is 'Visibility' (type 'Visibility')

这是我的编辑模板:

<dxg:GridControl.Resources>
    <ControlTemplate x:Key="PermissionEditorTemplate">
        <ui:FlagEnumEdit Name="PART_Editor" Style="{StaticResource FlagEnumEditStyle}" Target="{Binding EditValue, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
    </ControlTemplate>
</dxg:GridControl.Resources>

我尝试在我的风格中使用RelativeSource={RelativeSource TemplatedParent}但是绑定根本不再起作用。 ItemsSource绑定确实很有效,因为当我测试它时,我的所有复选框都会出现。但是当我尝试检查一个时,弹出窗口关闭,目标属性根本没有改变。

在内联控件时,我不确定为什么这不起作用。而且我担心它为什么会部分工作。

这里是班级本身(Pastebin)以备不时之需。 Haven还没有清理整个班级,当我不适合这样做时就这样做了,所以它非常混乱。

0 个答案:

没有答案