wpf ItemsControl中UserControl的DataContext变为空

g6ll5ycj  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(230)

我有一个ItemsControl如下。我想在其中添加不同的UserController。我为此创建了一个数据模板。我正在将我的项添加到我绑定在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;
    }
}

UserControl.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>

UserControlViewModel

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出现了两次。

xkftehaa

xkftehaa1#

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

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

相关问题