Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 动态创建的带多绑定和RelativeSource->Unset值的选项卡项_Wpf_Xaml_Dynamic_Tabitem_Multibinding - Fatal编程技术网

Wpf 动态创建的带多绑定和RelativeSource->Unset值的选项卡项

Wpf 动态创建的带多绑定和RelativeSource->Unset值的选项卡项,wpf,xaml,dynamic,tabitem,multibinding,Wpf,Xaml,Dynamic,Tabitem,Multibinding,在我的WPF应用程序中,我希望在包含TabControl的可重用UserControl中动态创建TabItems TabControl的ItemsSource通过标准数据绑定绑定到ObservableCollection实例 调用InitializeComponent后,我创建了如下选项卡项 // ... int itemCount = 0; TabItem it = null; it = new TabItem(); it.Header = "Sicherungen + Relais"; t

在我的WPF应用程序中,我希望在包含TabControl的可重用UserControl中动态创建TabItems

TabControl的ItemsSource通过标准数据绑定绑定到ObservableCollection实例

调用InitializeComponent后,我创建了如下选项卡项

// ...
int itemCount = 0;
TabItem it = null;

it = new TabItem();
it.Header = "Sicherungen + Relais";
tabItemList.Insert(itemCount++, it);            

it = new TabItem();
it.Header = "Lage der Bauteile";
tabItemList.Insert(itemCount++, it);

it = new TabItem();
it.Header = "Schaltpläne";
tabItemList.Insert(itemCount++, it);

it = new TabItem();
it.Header = "Tipps + Tricks";
tabItemList.Insert(itemCount++, it);
好消息是,这些项确实被添加到TabControl中,并带有各自的标题

现在,当WPF在窗口变为可见后试图对其应用样式时,问题就出现了

对于选项卡项,我有以下默认样式:

<Style TargetType="{x:Type TabItem}"
       BasedOn="{StaticResource TISTabItem}">
    <Setter Property="Width">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource convCategoryTabWidthConverter}">
                <Binding RelativeSource="{RelativeSource AncestorType={x:Type TabControl}, Mode=FindAncestor}" />
                <Binding RelativeSource="{RelativeSource AncestorType={x:Type TabControl}, Mode=FindAncestor}"
                         Path="ActualWidth" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>
和转换器:

public class CategoryTabWidthConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values == null || values.Count() < 1)
            throw new ArgumentException("Values array passed is invalid.", "values");

        if(values[0] == DependencyProperty.UnsetValue)
        {
            return 0; // Added for breakpoint. Bailing out here!!!
        }

        TabControl tabCtrl = values[0] as TabControl;
        Double w = (tabCtrl.ActualWidth / tabCtrl.Items.Count);
        w = (w <= 1) ? 0.0 : (w - 1);
        return w;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
备注:我在另外两个地方使用这个转换器和样式,其中TabItems是在XAML中静态添加的!在这些情况下,一切都很好

但是,当动态添加TabItems时,TabControl RelativeSource及其实际宽度都会计算为DependencyProperty.UnsetValue

我希望在这里遇到一些逻辑错误

什么时候应用样式?在将选项卡项正确添加到内部树之前还是之后? 有人知道我做错了什么吗


我会进一步调查,并提前感谢你们的帮助。

我自己解决了这个问题。问题是我忘记了ItemsSource的元素将被包装到TabItem实例中

TabItem中的TabItem实际上没有意义

直接将它们插入TabControl。Items使其工作起来令人惊讶