【问题标题】:How to change databinding of TextBlock by Click into ItemsControl MVVM?如何通过单击进入 ItemsControl MVVM 来更改 TextBlock 的数据绑定?
【发布时间】:2019-06-24 14:24:12
【问题描述】:

如何单击 1 个 ItemsControl 然后它会更改值文本但它不起作用

这是我在 View 中的代码 xaml:

<ItemsControl ItemsSource="{Binding FCsource}">
    <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border Margin="10" Width="440" Height="220" >
                                            <i:Interaction.Triggers>
                                                <i:EventTrigger EventName="MouseLeftButtonDown">
                                                    <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=DataContext.MouseLeftButtonDown }" >
                                                    </i:InvokeCommandAction>
                                                </i:EventTrigger>
                                            </i:Interaction.Triggers>
                                            <Grid>
                                                <TextBlock Text="{Binding Words}"></TextBlock>
                                            </Grid>
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
    </ItemsControl>

这是我在 ViewModel 中的代码 xaml:

public ObservableCollection _FCsource;

public ObservableCollection<Model.MenuItem> FCsource { get { return AddFlashCard(); } set { FCsource = value; OnPropertyChanged(); } }

private String _Words;
public string Words { get => _Words; set { _Words = value; OnPropertyChanged(); } }

public RelayCommand MouseLeftButtonDown { get; set; }

public WordsViewModel()
{
    MouseLeftButtonDown = new RelayCommand(() => MessageBox.Show("Message", "Hi"));
}

private ObservableCollection<MenuItem> AddFlashCard()
{
    ObservableCollection<MenuItem> listmn = new ObservableCollection<MenuItem>();
    listmn.Add(new MenuItem("qwda"));
    listmn.Add(new MenuItem("qwda"));
    return listmn;
}

【问题讨论】:

  • 你的代码在哪里?
  • @NamDang 只是发布有问题的代码,有人会编辑它
  • 我将它添加到问题中
  • 那么itemscountrol的来源是怎么设置的呢?你把那部分剪掉了。
  • 是的,我剪了

标签: c# wpf wpf-controls


【解决方案1】:

在你的 DataTemplate 中写一个简单的按钮怎么样,然后在里面放你的 TextBlock :

<Button Command="YourRelayCommand">
    <TextBlock Text="{Binding Words}" />
</Button>

【讨论】:

  • 但我想点击 ItemsControl 然后它改变值。感谢您的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 2019-12-03
相关资源
最近更新 更多