gpt4 book ai didi

绑定(bind)路径中的 WPF 强制转换

转载 作者:行者123 更新时间:2023-12-04 17:34:33 27 4
gpt4 key购买 nike

这是与 WPF Binding : Casting in binding path 类似的问题。 ,我需要在 XAML 绑定(bind)语句中转换一个对象。但我似乎无法理解如何在我的特定情况下制作绑定(bind)。

该问题的答案引用 PropertyPath XAML Syntax ,相关部分是(我相信)Single Property, Attached or Otherwise Type-Qualified .

就我而言,在我的主视图模型中,我有一个字典,将字符串映射到实现基本接口(interface)的对象:

Dictionary<string, IFlintStone> FlintStones { get; set;}

public interface IFlintStone { Walk, Talk etc}
public class FlintStone : IFlintStone { .. }

但是,我还有这些附加对象和接口(interface),它们是基对象的子类:
public interface IFred : IFlintStone { Eat, Drink, Yell etc }
public interface IWilma : IFlintStone { Wash, Clean, Cook etc }

public class Fred : FlintStone, IFred {..}
public class Wilma : FlintStone, IWilma {..}

最终我像这样填充我的字典:
FlintStones["Fred"] = new Fred();
FlintStones["Wilma"] = new Wilma();

现在,在我的 XAML 中,我有一个用于呈现 Fred 的用户控件。对象,另一个用于渲染 Wilma目的。我可以设置这些用户控件的数据上下文,例如:
<FredControl DataContext="{Binding Path=FlintStones[Fred]}" />
<WilmaControl DataContext="{Binding Path=FlintStones[Wilma]}" />

但是我的理解是,这只会暴露 IFlintStone这些对象的组件到它们各自的用户控件。但是我要曝光 IFred<FredControl>IWilma<WilmaControl>
这可能吗?在这种情况下,绑定(bind)语法是什么?

使用我上面引用的链接中的想法,我尝试了以下方法:
<FredControl DataContext="{Binding path=(myns:Fred.FlintStones[Fred])}" />


<FredControl DataContext="{Binding path=(myns:Fred).FlintStones[Fred]}" />

(其中 myns 是一个 xaml 命名空间定义,指向程序集中的 Fred 对象。)

但是程序要么在启动时崩溃并烧毁,要么提示找不到 Fred。作为当前数据上下文的属性。

最佳答案

这是我对您的问题的解释的工作版本:

主窗口.xaml

<Window x:Class="WpfApplication22.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication22"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock Text="{Binding Path=FlintStones[Fred].FlintStone}" />
<TextBlock Text="{Binding Path=FlintStones[Fred].(local:Fred.Yell)}" />
<local:FredControl DataContext="{Binding Path=FlintStones[Fred]}" />
<TextBlock />
<TextBlock Text="{Binding Path=FlintStones[Wilma].FlintStone}" />
<TextBlock Text="{Binding Path=FlintStones[Wilma].(local:Wilma.Clean)}" />
<local:WilmaControl DataContext="{Binding Path=FlintStones[Wilma]}" />
</StackPanel>
</Window>

主窗口.xaml.cs
using System.Windows;
using System.Collections.Generic;

namespace WpfApplication22
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public Dictionary<string, IFlintStone> FlintStones { get; set; }

public MainWindow()
{
InitializeComponent();

FlintStones = new Dictionary<string, IFlintStone>();
FlintStones["Fred"] = new Fred();
FlintStones["Wilma"] = new Wilma();

this.DataContext = this;
}
}

public interface IFlintStone
{
string FlintStone { get; set; }
}
public interface IFred : IFlintStone
{
string Yell { get; set; }
}
public interface IWilma : IFlintStone
{
string Clean { get; set; }
}

public class Fred : IFred
{
public string FlintStone { get; set; }
public string Yell { get; set; }

public Fred()
{
FlintStone = "Fred Flintstone";
Yell = "Yabba Dabba Doo";
}
}

public class Wilma : IWilma
{
public string FlintStone { get; set; }
public string Clean { get; set; }

public Wilma()
{
FlintStone = "Wilma Flintstone";
Clean = "Mammoth Dish Washer";
}
}
}

FredControl.xaml
<UserControl x:Class="WpfApplication22.FredControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Background="Beige">
<TextBlock Text="{Binding FlintStone}" />
<TextBlock Text="{Binding Yell}" />
</StackPanel>
</UserControl>

WilmaControl.xaml
<UserControl x:Class="WpfApplication22.WilmaControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Background="AntiqueWhite">
<TextBlock Text="{Binding FlintStone}" />
<TextBlock Text="{Binding Clean}" />
</StackPanel>
</UserControl>

FredControl.xaml.cs 和 WilmaControl.xaml.cs 未修改(只是 InitializeComponent(); 在构造函数中)。

和截图:

enter image description here

关于绑定(bind)路径中的 WPF 强制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26244400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com