我正试图绑定一个按钮,在左键单击打开其上下文菜单.我知道如何以编程方式执行此操作,但有没有办法使用默认命令绑定执行此操作?
我现在有:
<Button Command="ApplicationCommands.ContextMenu">
<Button.ContextMenu>
<ContextMenu>
<MenuItem ...
Run Code Online (Sandbox Code Playgroud)
但没有这样的运气...如果我这样做,那么按钮被禁用.我想这表明该命令无法执行,但为什么呢?
在编写JAXWS客户端时,这是我过去使用的:
// CALL SERVICE
EPaymentsService bPayService = new EPaymentsService();
ServiceInterface stub = bPayService.getPort();
BindingProvider bp = (BindingProvider) stub;
Map<String, Object> rc = bp.getRequestContext();
String endPointUrl = propFile.getString(Constants.END_POINT_URL);
rc.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endPointUrl);
// RESPONSE
ResponseMessage resMessage = stub.sendMessage(reqMessage);
Run Code Online (Sandbox Code Playgroud)
在我的代码中,ServiceInterface不会扩展BindingProvider.So为什么我们在转换时不会出错
BindingProvider bp = (BindingProvider) stub;
Run Code Online (Sandbox Code Playgroud) 我不明白为什么在我的以下示例中,"Billing Model"组合框在文本框中没有显示属性BillingModel.BillingModelDescription.选择客户端后,我希望组合框显示当前的billng模型描述,但它保持空白.绑定到同一事物的文本框确实显示了描述.我有一些可能的模型作为ItemsSource,它工作正常.如何在选择客户端时更新计费模型组合框?
这是XAML:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Client"/>
<ComboBox ItemsSource="{Binding AllClientData}" DisplayMemberPath="EmployerStr"
SelectedItem="{Binding SelectedClient}"
Width="300"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Billing Model:"/>
<ComboBox ItemsSource="{Binding AllBillingModels}" DisplayMemberPath="BillingModelDescription"
SelectedItem="{Binding SelectedClient.BillingModel}"
Width="300"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Billing Model" />
<TextBox Text="{Binding SelectedClient.BillingModel.BillingModelDescription}" Width="200"/>
</StackPanel>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
代码隐藏(这只是一个例子,我在完整的应用程序中使用MVVM等,但这是我用来说明问题):
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
AllClientData = new ObservableCollection<ClientRate>();
AllBillingModels = new ObservableCollection<BillingModelType>();
ClientRate uno = new ClientRate();
uno.BillingModel = new BillingModelType();
uno.BillingModel.BillingModelID = 3; …Run Code Online (Sandbox Code Playgroud) 我窗口的XAML看起来像这样:
<Window x:Class="Binding1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Cronjobs" Height="350" Width="525">
<Grid>
<ListBox HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" ItemsSource="{Binding Cronjobs}" />
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
可见我将ListBox绑定ItemsSource到当前Cronjobs属性DataContext.DataContext在代码隐藏的构造函数中设置为以下ViewModel的实例:
public partial class MainWindow : Window
{
private CronjobViewModel cronjobViewModel;
public MainWindow()
{
InitializeComponent();
this.cronjobViewModel = new CronjobViewModel();
this.DataContext = cronjobViewModel;
}
}
Run Code Online (Sandbox Code Playgroud)
ViewModel看起来像这样:
class CronjobViewModel : DependencyObject
{
public ObservableCollection<Cronjob> Cronjobs;
public CronjobViewModel( )
{
this.Cronjobs = new ObservableCollection<Cronjob>();
this.Cronjobs.Add( new Cronjob() );
this.Cronjobs.Add( new Cronjob() );
}
}
Run Code Online (Sandbox Code Playgroud)
为了快速简单的调试,我现在手动将一些项目添加到集合中.那Cronjob类是实际的模型,它只不过是一些简单的字符串属性,砍倒在主要部件的类的更多:
class Cronjob …Run Code Online (Sandbox Code Playgroud) 我们有一个带有简单Text属性的TickerUserControl,它代表代码的显示文本。
我们是否真的必须在UserControl中使用这些DependencyProperty模式(见下文),还是有一种更简单的方法来实现这一点?
当我们要使用UserControl和BIND将文本字段绑定到ViewModel的属性时,我们必须使用以下奇怪的绑定语法。为什么我们不能像其他所有控件一样只使用'Text =“ {Binding Text}”“'?UserControl的属性实现有问题吗?
UserControl的用法
<userControls:TickerUserControl Text="{Binding Path=Parent.DataContext.TickerText, RelativeSource={RelativeSource Self}, Mode=OneWay}"/>
Run Code Online (Sandbox Code Playgroud)
UserControl的属性实现(后面的代码)
public partial class TickerUserControl : UserControl
{
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TickerUserControl), new PropertyMetadata(""));
// ...
}
Run Code Online (Sandbox Code Playgroud)
UserControl的XAML代码段
<UserControl x:Class="Project.UserControls.TickerUserControl"
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"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d">
<TextBlock Text="{Binding Text}">
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
解决方案
问题是在UserControl内部设置了DataContext。我删除了DataContext绑定,为UserControl添加了名称,并修改了UserControl内部的TextBox绑定。之后,我可以从外面“照常”绑定。
<userControls:TickerUserControl Text="{Binding TickerText}"/>
<UserControl x:Class="Project.UserControls.TickerUserControl"
Name="TickerUserControl"
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"> …Run Code Online (Sandbox Code Playgroud) 我之前在RowDetailsTemplate中发布了带有DataGrid的WPF DataGrid.现在我只想弄清楚一个部分.
我有一个绑定到作业列表的DataGrid.在每个作业模型上都是Employees和SelectedEmployee属性的列表.
public class Job : _Base
{
private string _JobName = string.Empty;
public string JobName
{
get { return _JobName; }
set
{
if (_JobName != value)
{
_JobName = value;
RaisePropertyChanged("JobName");
}
}
}
private string _JobNumber = string.Empty;
public string JobNumber
{
get { return _JobNumber; }
set
{
if (_JobNumber != value)
{
_JobNumber = value;
RaisePropertyChanged("JobNumber");
}
}
}
private ObservableCollection<Employee> _Employees;
public ObservableCollection<Employee> Employees
{
get { return _Employees; …Run Code Online (Sandbox Code Playgroud) In a LongListSelector, I have multiple items shown, according to the following DataTemplate :
<TextBlock Text="{Binding Subject}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<StackPanel Orientation="Horizontal">
<TextBlock Text="Last modified :" Margin="15, 0, 5, 0" Foreground="LightGray" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding LastModified}" Foreground="#989696" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
At this point, everything works fine, the MVVM and bindings are OK.
I wanted to move this XAML into an UserControl and bind those properties from it. And, I have thought to proceed in this way :
<UserControl …Run Code Online (Sandbox Code Playgroud) 我是WPF的新手,我正在玩Bindings.我设法将绑定设置为a List,以便显示例如网格中的人员列表.我现在想要的是在Binding上设置一个条件,并且只选择满足这个条件的网格中的人.到目前为止我所拥有的是:
// In MyGridView.xaml.cs
public class Person
{
public string name;
public bool isHungry;
}
public partial class MyGridView: UserControl
{
List<Person> m_list;
public List<Person> People { get {return m_list;} set { m_list = value; } }
public MyGridView() { InitializeComponent(); }
}
// In MyGridView.xaml
<UserControl x:Class="Project.MyGridView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<DataGrid Name="m_myGrid" ItemsSource="{Binding People}" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我现在想要的是,只在Person饥饿的列表实例中包含.我知道在代码中执行此操作的方法,例如添加新属性:
public List<Person> HungryPeople
{
get
{
List<Person> hungryPeople = new List<Person>();
foreach (Person person in People)
if …Run Code Online (Sandbox Code Playgroud) 我有2个元素 - 窗口和按钮.
我想绑定Window的BorderBrush价值的Button的isPressed Background价值.
两个元素都有自定义样式.这里是样式:
按钮样式:
<Style x:Key="TitleBarButton" TargetType="Button">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Padding" Value="12,8.5"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="FontFamily" Value="Marlett"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
<Grid>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextBlock.FontFamily="{TemplateBinding FontFamily}" TextBlock.FontSize="{TemplateBinding FontSize}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#EFEFF2" />
</Trigger>
<Trigger …Run Code Online (Sandbox Code Playgroud) 我有这个非常简单的PHP代码:
$mysqli = new mysqli('localhost', 'xxx', 'xxxxx', 'xxx');
$query = "SELECT * FROM questions WHERE id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('d', $_GET['qid']);
$stmt->execute();
$stmt->bind_result($id, $content, $correct_ans, $lol);
$stmt->fetch();
//do sth with the data
$query = "SELECT * FROM answers WHERE question_id = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param('d', $_GET['qid']);
$stmt->execute();
$stmt->bind_result($id, $content, $lol);
while($stmt->fetch())
{
//do sth
}
Run Code Online (Sandbox Code Playgroud)
基本上,似乎无论我做什么,第二个mysqli::prepare()总会返回false,但mysqli::error由于某种原因是空的.谁能在这里看到错误?
PS.这不是一个重复的问题; 是的,已经有人问过:MySQLi准备好的声明返回false,但作者并没有费心与大家分享他的解决方案.
编辑:我想我应该解释一下:只有第二次准备返回false,第一次绝对没问题.更奇怪的是,如果我删除第一行代码(即第一次准备),第二行将毫无问题地工作......
EDIT2:嗯,我现在可以说的是WTF.事实证明,如果我删除了赋值,(我不做$stmt = prepare()...)但只是调用函数,$mysqli->error列表不是空的 - 它说"命令不同步;你现在不能运行这个命令".如果我做了分配, 它是空的...
编辑3:我切换到PDO,它完美无缺.除非我能证明不是这样,否则我会认为MySQLi是错误的.