ItemsControl中UserControl的DataContext变空

问题描述 投票:0回答:1

我有一个 ItemsControl 如下。我想在其中添加不同的 UserController。我为此创建了一个 DataTemplate。我正在将我的项目添加到我在 ViewModel 中绑定的 ToolItems 中。但是 UserControl 的 DataContext 变空了。由于 UserControl 中的所有内容都依赖于绑定,因此它进行了空插入。不幸的是,我不明白为什么。

 <ItemsControl ItemsSource="{Binding ToolItems}"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top">
        <ItemsControl.Resources>
            <DataTemplate DataType="{x:Type controlsViewModel:TextControlViewModel}">
                <controls:TextControl/>
            </DataTemplate>
        </ItemsControl.Resources>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas MouseMove="Canvas_MouseMove"
                        Width="100"
                        Height="100"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left"
                        Value="{Binding Path=Position.X}" />
                <Setter Property="Canvas.Top"
                        Value="{Binding Path=Position.Y}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>

主视图模型

public ObservableCollection<ViewModelBase> ToolItems
{
    get => _toolItems;
    set => SetProperty(ref _toolItems, value);
}

private void AddTool(object param)
{
    TextControlProperties properties = new TextControlProperties()
    {

        Description = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
        Width = 150,
        DescriptionWidth = 300,
        Margin = new Thickness(50),
        Height = 46,
        HelperText = "Helper Text",
        ParameterName = "Test",
        ControlStyle = (Style)Application.Current.Resources["MaterialDesignFloatingHintTextBox"],
    };
    switch (SelectedToolType.Type)
    {
        case ControlType.Text:
            var item = new TextControlViewModel();
            item.Properties = properties;
            ToolItems.Add(item);
            break;
    }
}

用户控件.xaml

    <UserControl.DataContext>
    <controlViewModel:TextControlViewModel />
</UserControl.DataContext>

<UserControl.Resources>
    <converters:NullOrEmptyOrWhiteSpaceVisibilityConverter x:Key="NullOrEmptyOrWhiteSpaceVisibilityConverter" />
</UserControl.Resources>
<Grid>
    <StackPanel Margin="{Binding Properties.Margin}"
                Canvas.Left="{Binding Position.X}"
                Canvas.Top="{Binding Position.Y}">
        <TextBlock Text="{Binding Properties.Title}"
                   FontSize="{Binding Properties.TitleFontSize}"
                   Visibility="{Binding Properties.Title, Converter={StaticResource NullOrEmptyOrWhiteSpaceVisibilityConverter}}"
                   Style="{StaticResource MaterialDesignSubtitle2TextBlock}"
                   HorizontalAlignment="Left" />
        <TextBlock Text="{Binding Properties.Description}"
                   FontSize="{Binding Properties.DescriptionFontSize}"
                   Opacity="0.7"
                   Visibility="{Binding Properties.Description, Converter={StaticResource NullOrEmptyOrWhiteSpaceVisibilityConverter}}"
                   TextWrapping="Wrap"
                   MaxWidth="{Binding Properties.DescriptionWidth}"
                   HorizontalAlignment="Left" />
        <TextBox ToolTip="{Binding Properties.ParameterName}"
                 Width="{Binding Properties.Width}"
                 Height="{Binding Properties.Height}"
                 FontSize="{Binding Properties.FontSize}"
                 materialDesign:TextFieldAssist.PrefixText="{Binding Properties.PrefixText}"
                 materialDesign:TextFieldAssist.SuffixText="{Binding Properties.SuffixText}"
                 materialDesign:HintAssist.Hint="{Binding Properties.Title}"
                 materialDesign:HintAssist.HelperText="{Binding Properties.HelperText}"
                 materialDesign:HintAssist.HelperTextFontSize="{Binding Properties.HelperTextFontSize}"
                 Style="{Binding Properties.ControlStyle}"
                 IsReadOnly="True"
                 HorizontalAlignment="Left">
        </TextBox>
    </StackPanel>
</Grid>

用户控制视图模型

    public class TextControlViewModel : ViewModelBase
{
    private TextControlProperties _properties;

    public TextControlProperties Properties
    {
        get => _properties;
        set => SetProperty(ref _properties, value);
    }

    private Point position;
    public Point Position
    {
        get { return position; }
        set { position = value; OnPropertyChanged("Position"); }
    }

    public TextControlViewModel() 
    {
    
    }
}

我不知道为什么,但我的 UserControlViewModel 出现了两次。

wpf user-controls itemscontrol
1个回答
0
投票

问题的根源其实很简单。即使我在 MainViewModel 中绑定了 DataContext,我还是在 UserControl.xaml 中再次创建它。所以第二个是创建一个空控件。当我在 UserControl.xaml 中删除下面的代码时,它已修复。

<UserControl.DataContext>
    <controlViewModel:TextControlViewModel />
</UserControl.DataContext>
© www.soinside.com 2019 - 2024. All rights reserved.