简体   繁体   English

在TabControl中将StaticResource与ItemSource混合

[英]StaticResource mixed with ItemSource in TabControl

I have an UserControl. 我有一个UserControl。 At the top, there is a global parameter, bound to a static property in the class MultiSliceCommand . 在顶部,有一个全局参数,绑定到类MultiSliceCommand的静态属性。 Below, there is a TabControl, populated by a Template and bound to public static ObservableCollection<GroupContainer> groups , also a property in MultiSliceCommand . 在下面,有一个TabControl,由一个Template填充,并绑定到public static ObservableCollection<GroupContainer> groups ,这也是MultiSliceCommand一个属性。 GroupContainer contains various properties, mainly doubles, ints etc., displayed and editable in textboxes in the TabItems. GroupContainer包含各种属性,主要是double,int等,这些属性在TabItems的文本框中显示和可编辑。

When I now change a value in TabItem, the corresponding property in the correct element of groups is set. 现在,当我更改TabItem中的值时,将在正确的groups元素中设置相应的属性。 However, when I close & reopen the dialog, the all the GroupContainers in groups are reset to their defaults - even the properties not bound at any point to the dialog. 但是,当我关闭并重新打开对话框时, groups中的所有GroupContainers都将重置为其默认值-即使该属性在任何时候都未绑定到对话框。

Changes to the global variables (outside of the TabControl) are preserved correctly. 正确保留对全局变量(在TabControl外部)的更改。 Changes to the TabControl are also preserved correctly if I remove the binding to the global variables - in explicit, if I remove the lines <local:MultiSliceCommand x:Key="mutliSliceCommand" /> and <TextBox x:Name="Mm_Per_Package" Text="{Binding Source={StaticResource mutliSliceCommand}, Path=Mm_Per_Package}" /> 如果我删除对全局变量的绑定,也可以正确保留对TabControl的更改-显式地,如果我删除行<local:MultiSliceCommand x:Key="mutliSliceCommand" /><TextBox x:Name="Mm_Per_Package" Text="{Binding Source={StaticResource mutliSliceCommand}, Path=Mm_Per_Package}" />

How can I change the bindings to preserve the changes to the global variable as well as the contents of the Tabs when closing & reopening the dialog? 关闭和重新打开对话框时,如何更改绑定以保留对全局变量以及选项卡内容的更改?

The Xaml File: Xaml文件:

<UserControl.Resources>
    <DataTemplate x:Key="HeaderTemplate">
        <Label Content="{Binding Group_Name}" />
    </DataTemplate>

    <local:MultiSliceCommand x:Key="mutliSliceCommand" />

    <DataTemplate x:Key="ItemTemplate">
        <Grid>
            <TextBox x:Name="_length" Text="{Binding Path=Length, UpdateSourceTrigger=PropertyChanged, Delay=0}"  />
        </Grid>
    </DataTemplate>
</UserControl.Resources>

<ScrollViewer>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <GroupBox
            Header="Global Parameters"
            Grid.Row="0"
            Grid.Column="0"
            >
            <Grid Height="Auto" Width="Auto">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <TextBox x:Name="Mm_Per_Package" Text="{Binding Source={StaticResource mutliSliceCommand}, Path=Mm_Per_Package}" />
            </Grid>
        </GroupBox>

        <GroupBox
            Header="Materials"
            Grid.Row="1"
            Grid.Column="0"
            Grid.ColumnSpan="2"
            >
            <TabControl x:Name="TabControl1"
            HorizontalAlignment="Left" 
            VerticalAlignment="Top"
            ItemTemplate="{StaticResource HeaderTemplate}" 
            ContentTemplate="{StaticResource ItemTemplate}"
                        />
        </GroupBox>
        <!--
        <Button Content="Save settings"
            Grid.Row="2"
            HorizontalAlignment="Right"
            Margin="10,10,0,0"
            VerticalAlignment="Top"
            Width="75" 
            Click="Btn_Save"    />-->
    </Grid>
</ScrollViewer>

The Class MultiSliceCommand MultiSliceCommand

public class MultiSliceCommand
{
    public static ObservableCollection<GroupContainer> groups { get; set; }
    private static double _mm_per_package { get; set; } = 0;
    public static double Mm_Per_Package
    {
        get { return _mm_per_package; }
        set { _mm_per_package = value < 0 ? 0 : value; }
    }

    public MultiSliceCommand()
    {
       groups = new ObservableCollection<GroupContainer>
        {
            new GroupContainer("Group 1"),
            new GroupContainer("Group 1"),
            new GroupContainer("Group 3")
        };
    }  
}

The class ObjectContainer ObjectContainer

public class GroupContainer : INotifyPropertyChanged
{
   private double _length { get; set; } = 0;


    public double Length
    {
        get { return _length; }
        set { _length = value < 0 ? 0 : value;  NotifyPropertyChanged("Min_Vector_Length"); }
    }

    // Methods
    public GroupContainer(string group_name)
    {
              }


    // Helper Stuff
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string sProp)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(sProp));
        }
    }
}

Ok, fixed it with an (somewhat dirty) hack: 好的,使用(有些肮脏的)hack修复了它:

I just outsourced the global variable to its own class, and bind the xaml to this class. 我只是将全局变量外包给自己的类,并将xaml绑定到此类。 In MultiSliceCommand , I use getter / setter on the property to just relay the value from the "isolation class" MultiSliceCommand ,我在属性上使用getter / setter来中继“隔离类”中的值

Isolation class: 隔离等级:

public class xaml_backend_variables
{
    private static double _mm_per_package = 0;
    public static double Mm_Per_Package
    {
        get { return _mm_per_package; }
        set { _mm_per_package = value < 0 ? 0 : value; }
    }

    public xaml_backend_variables()
    {

    }
}

MultiSliceCommand MultiSliceCommand

 public static double Mm_Per_Package
    {
        get { return xaml_backend_variables.Mm_Per_Package; }
        set { xaml_backend_variables.Mm_Per_Package = value; }
    }

XAML Modifications XAML修改

....
<local:xaml_backend_variables x:Key="xaml_backend_variables" />
....
<TextBox x:Name="Mm_Per_Package" Text="{Binding Source={StaticResource xaml_backend_variables}, Path=Mm_Per_Package}" />

But now all values are preserved correctly when closing and reopening the dialog. 但是现在,在关闭并重新打开对话框时,所有值都可以正确保留。

Still, if someone has an explanation why this happens and what would be the correct / elegant way to solve this, I would like very much to know! 不过,如果有人能解释为什么会发生这种情况,以及解决该问题的正确/优雅方法是什么,我非常想知道!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM