我有有兩件事情XAML文件,自定義數據綁定到一個以上的東西WPF C#
組合框
一個StackPanel
如何我使組合框中的更改將自動使堆疊面板從一個堆疊面板切換到另一個堆疊面板。
我的組合框是一樣的東西
<ComboBox x:Name="MCbConnect" SelectedIndex="{Binding EnConnectionType}" Loaded="m_cbConnect_Loaded" SelectionChanged="m_cbConnect_SelectionChanged" Width="100"></ComboBox>
其中EnConnectionType是一個屬性這樣
private ConnectionType _enConnectionType;
public ConnectionType EnConnectionType
{
get { return _enConnectionType; }
set { SetProperty(ref _enConnectionType, value, "EnConnectionType"); }
}
而且ConnectionType是
public enum ConnectionType { Rs232 = 0, Can = 1, Ethernet = 2 };
所以我已經實現INotifyChanged接口已經在這裏。但我不知道如何綁定這個數據與一個stackpanel容器,這將允許我在後臺自動切換到不同的堆疊面板視圖。一個XAML的
的例子,我想切換到的
<GroupBox x:Class="Gui.CtrlCommSocketSettings"
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:src="clr-namespace:Akribis.Gui"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Header="Comm Settings"
mc:Ignorable="d"
d:DesignHeight="80" d:DesignWidth="300">
<Grid Height="70" VerticalAlignment="Top">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,0,3,0"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Width" Value="120"/>
<Setter Property="Margin" Value="0,1"/>
</Style>
<Style TargetType="CheckBox">
<Setter Property="Margin" Value="0,4"/>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Server:" Name="MTextBlockServer"/>
<TextBlock Grid.Row="1" Text="Port:"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Server}" Name="MTextBoxServer"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Port}"/>
</Grid>
與一些CS文件中像
namespace Gui
{
public partial class CtrlCommSocketSettings
{
public CtrlCommSocketSettings()
{
InitializeComponent();
}
}
}
我不想因爲我知道要做到這一點編程我想避免模型和視圖之間的耦合。
的是什麼,我不想這樣做,但目前所面對的例如: 主XAML,我有一個空的StackPanel
<StackPanel Orientation="Vertical" Name="MCtrlCommSettings"></StackPanel>
,我很明確地做這樣的事情
加入到這個StackPanel中MCtrlCommSettings.Children.Clear();
MCtrlCommSettings.Children.Add(_serverCtrlCommSettings);
我該如何去做這個自動?就像InotifyChanges將如何自動在視圖和模型之間更新一樣。任何建議都會受到歡迎。
在線閱讀,看來我需要實現某種形式的可觀察名單
冠軍。非常感謝你,它完美的作品 – CJC