当其他 TextBox TextChanged 在同一列表框项 WPF 中触发时,从 ListBox 项更新 TextBox 文本

Update a TextBox Text from a ListBox Item when other TextBox TextChanged its triggered inside the same listbox item WPF

包含两个文本框的列表框项及其工作原理的图片:

问题是我得到了一个 ListBox 项目,其中包括两个文本框的一些对象,一个文本框是用来写入它的,当触发 TextBox TextChanged 时,它应该调用一个函数来更新第二个文本框是只读的。

下一个代码是 class,它用于为 ListBox 项目内的所有对象赋值。

当触发第一个文本框的 TextChanged 时,如何更新第二个文本框?谢谢!

public class ListBoxSneakerItems
{
    public string Image { get; set; }
    public string Name { get; set; }
    public string Size { get; set; }
    public string ID { get; set; }
    public string Status { get; set; }
    public double StorePrice { get; set; }
    public double PayoutPrice { 
        get { return "void call" } 
        }
}

您可以使用 ItemTemplate

这是我的代码 主窗口:

<Window x:Class="WpfApplication.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="5">
                        <TextBox x:Name="source" FontSize="16" Text="{Binding Path=ItemTitle}" HorizontalAlignment="Center" />
                        <TextBlock FontSize="16" Text="{Binding ElementName=source, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

商品型号:

public class ItemModel
{
    public string ItemTitle { get; set; }
}

和主要 window 视图模型:

public class MainWindowViewModel
{
    public ObservableCollection<ItemModel> Items { get; set; }

    public MainWindowViewModel()
    {
        Items = new ObservableCollection<ItemModel>
        {
            new ItemModel { ItemTitle = "exemple title 1" },
            new ItemModel { ItemTitle = "exemple title 2" },
        };
    }
}

结果:

更新:

和项目:

更新 2:

否则你可以使用 UserControl:

只需将模板移动到自定义控件并将 UserControl 设置为 ListBox 的模板

用户控件:

<UserControl x:Class="WpfApplication.Wpf.ItemBoxTemplate"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Background="White">
    <Grid>
        <StackPanel Margin="5">
            <TextBox x:Name="source" FontSize="16" Text="{Binding Path=ItemTitle}" HorizontalAlignment="Center" />
            <TextBlock FontSize="16" Text="{Binding ElementName=source, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" />
        </StackPanel>
    </Grid>
</UserControl>

和主窗口:

<Window x:Class="WpfApplication.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <ListBox ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <local:ItemBoxTemplate />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

其余代码保持不变