从 wpf 页面中的组合框(在用户控件中)获取数据
Get datas from a combobox(in a usercontrol) in a wpf page
在 WPF 应用程序中,我想从用户控件中的组合框获取数据并将其传递到页面中的文本块。
请注意,组合框的项目通过 EntityFramework 绑定到 SQL table。
这是用户控件:
<UserControl x:Class="TODO_ERP_AGRO.User_Controls.BartenderFormats_CB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="35" d:DesignWidth="800">
<StackPanel Orientation="Horizontal">
<Label Content="FORMATS BARTENDER"/>
<ComboBox x:Name="BartFormats_CB" Style="{StaticResource PlainComboBoxStyle}"
Width="250" IsEnabledChanged="BartFormats_CB_IsEnabledChanged"/>
</StackPanel>
</UserControl>
和UserControl的.cs
using System.Windows;
using System.Windows.Controls;
namespace TODO_ERP_AGRO.User_Controls
{
/// <summary>
/// Logique d'interaction pour BartenderFormats_CB.xaml
/// </summary>
public partial class BartenderFormats_CB : UserControl
{
public BartenderFormats_CB()
{
InitializeComponent();
Sql_Queries.SQLS.GetFormats_Bartender(BartFormats_CB);
}
public string BartenderFormat_Name
{
get { return BartFormats_CB.SelectedItem.ToString(); }
set { BartFormats_CB.SelectedItem = value; }
}
private void BartFormats_CB_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
switch (((ComboBox)sender).IsEnabled)
{
case false:
((ComboBox)sender).SelectedIndex = -1;
break;
default:
break;
}
}
}
}
组合框绑定
public static void GetFormats_Bartender(ComboBox cb)
{
using (UTILITAIRESEntities dc = new UTILITAIRESEntities())
{
cb.ItemsSource = (from a in dc.VW_BARTENDER_FORMATS
orderby a.MODELE_ETIQ
select new
{
a.ET_NUMSEQ,
a.MODELE_ETIQ
}).ToList();
cb.DisplayMemberPath = "MODELE_ETIQ";
cb.SelectedValuePath = "ET_NUMSEQ";
dc.Dispose();
}
}
以及 WPF 页面中的 UserContrpl
<UserControl x:Class="TODO_ERP_AGRO.User_Controls.Create_UC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ucs="clr-namespace:TODO_ERP_AGRO.User_Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="1100">
<Grid Background="{StaticResource SombreBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!--#region TYPE -->
<GroupBox Header="TYPE" Style="{StaticResource PlainGroupBoxStyle}">
<WrapPanel Orientation="Horizontal">
<RadioButton x:Name="Bartender_RB" Content="BARTENDER"
Style="{StaticResource PlainRadioButton_Style}"/>
<RadioButton x:Name="Crystal_RB" Content="CRYSTAL REPORT"
Style="{StaticResource PlainRadioButton_Style}"/>
<RadioButton x:Name="Autre_RB" Content="Autre"
Style="{StaticResource PlainRadioButton_Style}"/>
</WrapPanel>
</GroupBox>
<!--#endregion-->
<!--#region Details TYPE -->
<GroupBox Header="DETAILS" Grid.Row="1">
<WrapPanel Orientation="Horizontal">
<ucs:BartenderFormats_CB/>
</WrapPanel>
</GroupBox>
<!--#endregion-->
<StackPanel Grid.Row="2">
<Button x:Name="Test_Btn" Content="TEST" Click="Test_Btn_Click" Width="50"/>
<TextBlock x:Name="TestTxtBlock"/>
</StackPanel>
</Grid>
</UserControl>
.cs
private void Test_Btn_Click(object sender, RoutedEventArgs e)
{
BartenderFormats_CB cb = new BartenderFormats_CB();
TestTxtBlock.Text = cb.BartFormats_CB.ToString();
}
当我点击按钮时,没有附加任何内容(没有错误)。
谢谢你帮助我
当您将用户控件的一个实例放入 Window 时,您就创建了它的一个实例。
To access its members, simply assign a name to usercontrol and use it to access its members.
使用此代码:
<GroupBox Header="DETAILS" Grid.Row="1">
<WrapPanel Orientation="Horizontal">
<ucs:BartenderFormats_CB x:Name="myCMB"/>
</WrapPanel>
</GroupBox>
和您的代码:
private void Test_Btn_Click(object sender, RoutedEventArgs e)
{
TestTxtBlock.Text = myCMB.BartenderFormat_Name;
}
在 WPF 应用程序中,我想从用户控件中的组合框获取数据并将其传递到页面中的文本块。 请注意,组合框的项目通过 EntityFramework 绑定到 SQL table。 这是用户控件:
<UserControl x:Class="TODO_ERP_AGRO.User_Controls.BartenderFormats_CB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="35" d:DesignWidth="800">
<StackPanel Orientation="Horizontal">
<Label Content="FORMATS BARTENDER"/>
<ComboBox x:Name="BartFormats_CB" Style="{StaticResource PlainComboBoxStyle}"
Width="250" IsEnabledChanged="BartFormats_CB_IsEnabledChanged"/>
</StackPanel>
</UserControl>
和UserControl的.cs
using System.Windows;
using System.Windows.Controls;
namespace TODO_ERP_AGRO.User_Controls
{
/// <summary>
/// Logique d'interaction pour BartenderFormats_CB.xaml
/// </summary>
public partial class BartenderFormats_CB : UserControl
{
public BartenderFormats_CB()
{
InitializeComponent();
Sql_Queries.SQLS.GetFormats_Bartender(BartFormats_CB);
}
public string BartenderFormat_Name
{
get { return BartFormats_CB.SelectedItem.ToString(); }
set { BartFormats_CB.SelectedItem = value; }
}
private void BartFormats_CB_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
switch (((ComboBox)sender).IsEnabled)
{
case false:
((ComboBox)sender).SelectedIndex = -1;
break;
default:
break;
}
}
}
}
组合框绑定
public static void GetFormats_Bartender(ComboBox cb)
{
using (UTILITAIRESEntities dc = new UTILITAIRESEntities())
{
cb.ItemsSource = (from a in dc.VW_BARTENDER_FORMATS
orderby a.MODELE_ETIQ
select new
{
a.ET_NUMSEQ,
a.MODELE_ETIQ
}).ToList();
cb.DisplayMemberPath = "MODELE_ETIQ";
cb.SelectedValuePath = "ET_NUMSEQ";
dc.Dispose();
}
}
以及 WPF 页面中的 UserContrpl
<UserControl x:Class="TODO_ERP_AGRO.User_Controls.Create_UC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ucs="clr-namespace:TODO_ERP_AGRO.User_Controls"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="1100">
<Grid Background="{StaticResource SombreBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!--#region TYPE -->
<GroupBox Header="TYPE" Style="{StaticResource PlainGroupBoxStyle}">
<WrapPanel Orientation="Horizontal">
<RadioButton x:Name="Bartender_RB" Content="BARTENDER"
Style="{StaticResource PlainRadioButton_Style}"/>
<RadioButton x:Name="Crystal_RB" Content="CRYSTAL REPORT"
Style="{StaticResource PlainRadioButton_Style}"/>
<RadioButton x:Name="Autre_RB" Content="Autre"
Style="{StaticResource PlainRadioButton_Style}"/>
</WrapPanel>
</GroupBox>
<!--#endregion-->
<!--#region Details TYPE -->
<GroupBox Header="DETAILS" Grid.Row="1">
<WrapPanel Orientation="Horizontal">
<ucs:BartenderFormats_CB/>
</WrapPanel>
</GroupBox>
<!--#endregion-->
<StackPanel Grid.Row="2">
<Button x:Name="Test_Btn" Content="TEST" Click="Test_Btn_Click" Width="50"/>
<TextBlock x:Name="TestTxtBlock"/>
</StackPanel>
</Grid>
</UserControl>
.cs
private void Test_Btn_Click(object sender, RoutedEventArgs e)
{
BartenderFormats_CB cb = new BartenderFormats_CB();
TestTxtBlock.Text = cb.BartFormats_CB.ToString();
}
当我点击按钮时,没有附加任何内容(没有错误)。 谢谢你帮助我
当您将用户控件的一个实例放入 Window 时,您就创建了它的一个实例。
To access its members, simply assign a name to usercontrol and use it to access its members.
使用此代码:
<GroupBox Header="DETAILS" Grid.Row="1">
<WrapPanel Orientation="Horizontal">
<ucs:BartenderFormats_CB x:Name="myCMB"/>
</WrapPanel>
</GroupBox>
和您的代码:
private void Test_Btn_Click(object sender, RoutedEventArgs e)
{
TestTxtBlock.Text = myCMB.BartenderFormat_Name;
}