XAML 关于Win UI中的列表视图和图像绑定

ejk8hzay  于 2023-08-01  发布在  其他
关注(0)|答案(2)|浏览(78)

我有一个关于Win UI的查询,我希望有人能帮我解决这个问题。我对这个框架比较陌生,我正在研究列表视图,并重用了Microsoft文档中提供的一些代码。查找所附代码的修改版本。

<ListView
    x:Name="List"
    ItemsSource="{x:Bind ViewModel.List}"
    Width="400"
    SelectionChanged="ListView_SelectionChanged">
    <ListView.HeaderTemplate>
        <DataTemplate>
            <Grid
                Background="{ThemeResource SystemBaseHighColor}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="300" />
                    <ColumnDefinition Width="300" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Column 1" />
                <TextBlock
                    Grid.Column="1"
                    Text="Column 2" />
            </Grid>
        </DataTemplate>
    </ListView.HeaderTemplate>
    <ListView.ItemTemplate>
        <DataTemplate
            x:Name="TableDataTemplate"
            x:DataType="viewmodels:SampleObject">
            <Grid
                Height="50"
                AutomationProperties.Name="{x:Bind ObjectName}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="300" />
                    <ColumnDefinition Width="300" />
                </Grid.ColumnDefinitions>
                <TextBlock
                    Grid.Column="0"
                    VerticalAlignment="Center"
                    Text="{x:Bind ObjectName}" />
                <Image
                    Grid.Column="1"
                    Source="{x:Bind ObjectPath}"
                    HorizontalAlignment="Left" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

字符串
在这段代码中,我已经将图像的源代码绑定到了我希望它显示的路径。但是,如果我在运行时更改相应对象的路径,它会更新存储在视图模型中的数据。但是,它不更新图像绑定。我做错了什么吗?我如何才能使图像根据路径的值自动更新。是否应该调用一些方法来更新或刷新列表视图?或者我应该以不同的方式编写绑定代码?
非常感谢您的时间,提前!
我试着查看文档并阅读了他们突出显示的大多数属性,但似乎没有任何与我正在寻找的内容相关的内容。我也看了几个可能与此相关的线程,但找不到任何太相关的东西,尽管我可能忽略了它。

zf2sa74q

zf2sa74q1#

您需要在ViewModel中实现INotifyPropertyChanged
使用CommunityToolkit.Mvvm NuGet包,您的ViewModel可能是这样的。

public partial class SampleObject : ObservableObject
{
    // CommunityToolkit.Mvvm's source generator will create 
    // an UI interactive "ObjectName" property for you.
    [ObservableProperty]
    private string objectName = string.Empty;

    // An "ObjectPath" property will be created.
    [ObservableProperty]
    private string objectPath = string.Empty;
}

public partial class ViewModel : ObservableObject
{
    // A "List" property will be created.
    [ObservableProperty]
    private ObservableCollection<SampleObject> list = new();
}

字符串
使用x:Bind时,还需要将Mode设置为OneWay

am46iovg

am46iovg2#

下面是通过实现INotifyPropertyChanged编写的代码。请让我知道它是否正确。我是否需要以任何方式更改XAML中的绑定代码?谢谢你,谢谢

public partial class ViewModel : ObservableObject
{
    private ObservableCollection<SampleObject> list = new();

    public ViewModel()
    {
        // Constructor Logic
    }
}

public partial class SampleObject : ObservableObject, INotifyPropertyChanged
{
    [ObservableProperty]
    private string objectName = string.Empty;

    [ObservableProperty]
    private string objectPath = string.Empty;

    public SampleObject()
    {
        // Constructor Logic
    }

    public void UpdateObjectPath(String value)
    {
        // Update Object Path Logic
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public string ObjectPath
        {
            get => objectPath;

            set
            {
                if (value != objectPath)
                {
                    ObjectPath = value;
                    NotifyPropertyChanged();
                }
        }
    }
}

字符串

相关问题