gpt4 book ai didi

c# - 单一 View ,多个 View 模型 - 避免绑定(bind)错误?

转载 作者:行者123 更新时间:2023-12-03 10:35:14 26 4
gpt4 key购买 nike

有一个带有许多控件的 View (窗口),以简化:

<!-- edit property A -->
<TextBlock Text="A" ... />
<TextBox Text="{Binding Config.A}" ... />
<Button Command={Binding DoSometingWitA} ... />

<!-- edit property B -->
<TextBox Text="{Binding Config.B}" ... />

<!-- edit property C -->
<ComboBox Text="{Binding Config.C}" ... />

此 View 用于显示和编辑多个配置:
public class ViewModel: INotifyPropertyChanged
{
public BaseConfig Config {get {...} set {...}}
}

public class ConfigType1: BaseConfig { ... } // only has A
public class ConfigType2: BaseConfig { ... } // only has B
public class ConfigType3: BaseConfig { ... } // only has C
public class ConfigType4: BaseConfig { ... } // has A and B
public class ConfigType5: BaseConfig { ... } // has A and C

房产 可能可能不会存在某些配置。结果存在绑定(bind)错误。

问题:有没有办法隐藏当前 Config 中不存在的控件的属性?对象(这可以通过反射轻松完成)以及避免在 View 中出现绑定(bind)错误(这是实际问题,我不想重新发明 PropertyGrid 也不想使用)?

例如。如果 Config = new ConfigType1() (它只有 A 属性),那么 View 将只包含用于编辑属性 A 的控件, 控制编辑属性 B , C等应该是隐藏的,不会导致绑定(bind)错误。

如果有人愿意玩,这是一个测试用例。

XAML:
<TextBox Text="{Binding Config.A}" Visibility="Collapsed"/>
<TextBox Text="{Binding Config.B}" Visibility="Hidden"/>
<Button VerticalAlignment="Bottom"
Content="..."
Click="Button_Click" />

CS:
public partial class MainWindow : Window
{
public class BaseConfig { }

public class ConfigA : BaseConfig
{
public string A { get; set; }
}

public class ConfigB : BaseConfig
{
public string B { get; set; }
}

public BaseConfig Config { get; private set; }

public MainWindow()
{
InitializeComponent();
Config = new ConfigA() { A = "aaa" };
DataContext = this;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Config = new ConfigB() { B = "bbb" };
DataContext = null;
DataContext = this;
}
}

最初存在缺少 B 的绑定(bind)错误。 ,点击按钮后( ConfigB 将被赋值)出现绑定(bind)错误缺少 A .

如何避免这些错误?如果属性存在,可以通过检查反射来控制可见性(但仍然存在如何组织它的问题)。

最佳答案

您需要的是 DataTemplate。

工作样本:

 public BaseConfig Config { get; set; }

 <Window.Resources>
<DataTemplate DataType="{x:Type o:ConfigA}">
<!--
You can add here any control you wish applicable to ConfigA.
Say, a textbox can do.
-->
<TextBlock Text="{Binding A}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type o:ConfigB}">
<TextBlock Text="{Binding B}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type o:ConfigType10000000000}">
<superComplicatedControl:UniqueControl ProprietaryProperty="{Binding CustomProperty}"/>
</DataTemplate>
<!-- Rachel's point -->
<DataTemplate DataType="{x:Type o:Config4}">
<StackPanel>
<ContentControl Content="{Binding ConfigA}"/>
<ContentControl Content="{Binding ConfigB}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<ContentControl Content="{Binding Config}" />
<Button VerticalAlignment="Bottom" Content="woosh" Click="Button_Click" />
</StackPanel>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
// Config = new ConfigB() { B = "bbb" };
Config = new Config4() { ConfigA = (ConfigA) Config, ConfigB = new ConfigB { B = "bbb" } };
DataContext = null;
DataContext = this;
}

//…

// Rachel's point
public class Config4 : BaseConfig
{
public string A4 { get; set; }

public ConfigA ConfigA { get; set; }
public ConfigB ConfigB { get; set; }
}

关于c# - 单一 View ,多个 View 模型 - 避免绑定(bind)错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33480530/

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