wpf customControl binding itemscontrol DataTemplate button

后端 未结 2 1587
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 05:49

i am new to WPF so i am stuck with some binding on my customcontrol click button.

I have textbox that has watermark and selctedItems properties. Control if selectedi

相关标签:
2条回答
  • 2020-12-20 06:39

    First the viewmodel with the ObservableCollection and the Command that will execute the X:

    private ObservableCollection<string> items = new ObservableCollection<string>() { "One", "Two", "Three" };
        public ObservableCollection<String> Items
        {
            get
            {
                return items;
            }
            set
            {
                items = value;
                NotifyPropertyChanged();
            }
        }
    
        public Command<String> DeleteItem
        {
            get
            {
                return new Command<string>((item) =>
                {
                    if (items.Contains(item))
                    {
                        items.Remove(item);
                    }
                    NotifyPropertyChanged("Items");
                });
            }
        }
    

    Now the XAML:

    Resources, where it is binded to the 'parent' datacontext, this is a easy way to know where the binding is going:

    <Page.Resources>
        <DataTemplate x:Key="DefaultSelectedItemsTemplate" >
            <Border x:Name="selectedItemBorder" BorderBrush="Gray" BorderThickness="1" CornerRadius="5"  Margin="5,1,1,1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="15"/>
                    </Grid.ColumnDefinitions>
                    <!--<TextBlock Grid.Column="0" Text="{Binding RelativeSource={RelativeSource Self}, Path=Ime}" Margin="5,0,3,0"></TextBlock>-->
                    <TextBlock Grid.Column="0" Text="{Binding}" Margin="5,0,3,0"></TextBlock>
                    <Button x:Name="PART_selectedItemButton" BorderThickness="0" Grid.Column="1" Command="{Binding DataContext.DeleteItem, ElementName=ItemsControlInstance}" CommandParameter="{Binding}">X</Button>
                </Grid>
            </Border>
        </DataTemplate>
    </Page.Resources>
    

    And finally the itemscontrol:

    <ItemsControl x:Name="ItemsControlInstance" ItemTemplate="{StaticResource DefaultSelectedItemsTemplate}" ItemsSource="{Binding Items}"/>
    
    0 讨论(0)
  • 2020-12-20 06:40

    Finally i manage to make it work, with help and guidance from Jaun it still didnt work (i tried probably 10 different things and it was in DataContext .. it was never binded.

    So on my override OnApplyTemplate i added this.DataContext = this ... so i missed that part.

    I used AttachedCommandBehavior (nuget) for command

    code:

    public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.DataContext = this;
    
            ItemsControl itmControl = GetTemplateChild("PART_SelectedItemsHost") as ItemsControl;
    
            if (itmControl != null)
            {
                itmControl.MouseLeftButtonDown += new MouseButtonEventHandler(itmControl_MouseLeftButtonDown);
    
                // blind click on X buttons in ItemsControl
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题