因此,web和StackOverflow对于如何将组合框绑定到WPF中的枚举属性有很多很好的答案.但是Silverlight缺少使这成为可能的所有功能:(例如:
EnumDisplayer样式IValueConverter,因为Silverlight不支持x:Type.ObjectDataProvider像在此方法中那样使用,因为它在Silverlight中不存在.Type对象的属性来执行#1版本,因为XAML中不支持泛型(并且使它们工作的黑客都依赖于标记扩展,Silverlight不支持).大规模的失败!
在我看来,做这项工作的唯一方法是
IValueConverter为我想绑定的每个枚举制作一个自定义.是否有更通用的替代方案,即不要为我想要的每个枚举反复编写相同的代码?我想我可以使用接受枚举作为类型参数的泛型类来做解决方案#2,然后为我想要的每个枚举创建新类
class MyEnumConverter : GenericEnumConverter<MyEnum> {}
Run Code Online (Sandbox Code Playgroud)
伙计们,你有什么想法?
我有一个使用MVVM模式的WPF应用程序.将按钮连接到VM非常简单,因为它们实现了ICommand.我有一个类似的上下文菜单.下一步是为上下文菜单创建快捷键.我无法弄清楚如何让快捷键调用Command.这是一个例子:
<MenuItem Header="Update" Command="{Binding btnUpdate}" >
<MenuItem.Icon>
<Image Source="/Images/Update.png"
Width="16"
Height="16" />
</MenuItem.Icon>
</MenuItem>
Run Code Online (Sandbox Code Playgroud)
现在我添加了这个:
<Window.InputBindings>
<KeyBinding Key="U"
Modifiers="Control"
Command="{Binding btnUpdate}" />
</Window.InputBindings>
Run Code Online (Sandbox Code Playgroud)
尝试将快捷键连接到同一个绑定,但这不起作用.错误是:
错误169无法在"KeyBinding"类型的"Command"属性上设置"绑定".'绑定'只能在DependencyObject的DependencyProperty上设置.
有没有办法将此事件连接到命令?我无法弄清楚这一点.
提前致谢!
法案
在我的WPF应用程序中,我有许多数据绑定TextBoxes.在UpdateSourceTrigger这些绑定的LostFocus.使用"文件"菜单保存对象.我遇到的问题是可以在TextBox中输入一个新值,从File菜单中选择Save,并且永远不会保留新值(TextBox中可见的值)因为访问菜单不会从TextBox中删除焦点.我怎样才能解决这个问题?有没有办法强制页面中的所有控件数据绑定?
@palehorse:好点.不幸的是,我需要使用LostFocus作为我的UpdateSourceTrigger,以支持我想要的验证类型.
@dmo:我想到了这一点.然而,对于一个相对简单的问题,它似乎是一个非常不优雅的解决方案.此外,它要求页面上有一些控件,它始终可见以接收焦点.然而,我的应用程序是标签,因此没有这样的控件容易出现.
@Nidonocu:使用菜单没有从TextBox移动焦点的事实也使我感到困惑.然而,这就是我所看到的行为.以下简单示例演示了我的问题:
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider x:Key="MyItemProvider" />
</Window.Resources>
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="Save" Click="MenuItem_Click" />
</MenuItem>
</Menu>
<StackPanel DataContext="{Binding Source={StaticResource MyItemProvider}}">
<Label Content="Enter some text and then File > Save:" />
<TextBox Text="{Binding ValueA}" />
<TextBox Text="{Binding ValueB}" />
</StackPanel>
</DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
using System;
using System.Text;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication2
{
public partial class Window1 : Window
{
public MyItem Item
{
get …Run Code Online (Sandbox Code Playgroud) 我有一个简单的静态类,其中包含一些方法.每个方法都打开一个SqlConnection,查询数据库并关闭连接.这样,我确信我总是关闭与数据库的连接,但另一方面,我不喜欢总是打开和关闭连接.下面是我的方法的示例.
public static void AddSomething(string something)
{
using (SqlConnection connection = new SqlConnection("..."))
{
connection.Open();
// ...
connection.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
考虑到方法在静态类中,我是否应该有一个包含单个SqlConnection的静态成员?我该如何以及何时放弃它?什么是最佳做法?
我有一个文本框绑定到对象中的属性.我已将字符串格式设置为p0.
但是,当我输入12时,例如它被格式化为1200%(乘以100并添加%符号)
如何设置stringformat以便例如20格式化为20%?
我目前的控制是:
<TextBox Text="{Binding Path=MyCase, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, StringFormat=p0}"/>
Run Code Online (Sandbox Code Playgroud)
如何改变字符串格式,使7的格式为7%而不是700%?
如何在GridView中隐藏TemplateField列?
我尝试了以下方法:
<asp:TemplateField ShowHeader="False" Visible='<%# MyBoolProperty %>' >
<ItemTemplate>
<asp:LinkButton ID="attachmentButton" runat="server" ... />
</ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
但它不起作用,并给出以下错误:
仅在具有DataBinding事件的对象上支持数据绑定表达式.System.Web.UI.WebControls.TemplateField没有DataBinding事件.
我也尝试以编程方式隐藏它,但似乎不可能通过名称获取列,因为没有TemplateField列的名称.
我有以下XAML:
<Grid>
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ...>
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
<DockPanel Grid.Row="2">
<CheckBox x:Name="DisplayMarkers" DockPanel.Dock="Top" Content="Display Data Points?"
Margin="8,5,0,5" d:LayoutOverrides="Height" HorizontalAlignment="Left" IsChecked="False" />
<vf:Chart DockPanel.Dock="Top" ScrollingEnabled="False" ZoomingEnabled="True" ToolBarEnabled="True">
<vf:DataSeries AxisYType="Secondary" RenderAs="Line" DataSource="{Binding CdTeRoughnessList}"
XValueType="DateTime"
MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}" Color="Navy"
LegendText="Roughness Std. Dev.">
Run Code Online (Sandbox Code Playgroud)
此绑定失败: MarkerEnabled="{Binding ElementName=DisplayMarkers, Path=IsChecked}"
我正在尝试绑定到我的Checkbox上名为'DisplayMarkers'的IsChecked属性.当我运行它时,在VS 2010的调试模式下,输出窗口显示绑定失败.它找不到名为'Checkbox'的元素谁能告诉我为什么?
我从VS得到的错误是:
System.Windows.Data Error: 4 : Cannot find source for binding with reference
'ElementName=DisplayMarkers'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'DataSeries' (Name=''); target property is 'MarkerEnabled' (type 'Nullable`1')
Run Code Online (Sandbox Code Playgroud) 我是React.js的新手.我试图绑定数据数组.我正在寻找相当于ng-repeat,但我在文档中找不到它.
例如:
var data = ['red', 'green', 'blue']
Run Code Online (Sandbox Code Playgroud)
使用角度我会在我的HTML中做这样的事情:
<div ng-repeat="i in data">{{i}}</div>
Run Code Online (Sandbox Code Playgroud)
我想知道React的标记来做到这一点
有没有办法让另一个绑定作为后备值?
我正在尝试做这样的事情:
<Label Content="{Binding SelectedItem.Name, ElementName=groupTreeView,
FallbackValue={Binding RootGroup.Name}}" />
Run Code Online (Sandbox Code Playgroud)
如果有人有另一种技巧可以实现这一目标,那就太棒了.
我正在构建一个可供许多用户使用的应用程序.每个用户都被分类为下一个身份验证级别之一:
public enum AuthenticationEnum
{
User,
Technitian,
Administrator,
Developer
}
Run Code Online (Sandbox Code Playgroud)
某些控件(如按钮)仅向某些级别的用户公开.我有一个属性,其中包含当前用户的身份验证级别:
public AuthenticationEnum CurrentAuthenticationLevel { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想将此属性绑定到某些控件的"Visibilty"属性,并将参数传递给Converter方法,告诉它什么是能够查看控件的最低身份验证级别.例如:
<Button Visibility="{Binding Path=CurrentAuthenticationLevel, Converter={StaticResource AuthenticationToVisibility}, ConverterParameter="Administrator"}"/>
Run Code Online (Sandbox Code Playgroud)
意味着只有"管理员"和"开发人员"才能看到该按钮.不幸的是,上面的代码将"Administrator"作为字符串传递.当然,我可以在转换器方法中使用Switch-Case并将字符串转换为AuthenticationEnum.但这很难看并且容易出现维护错误(每次枚举更改时 - 转换器方法都应该更改).
有没有更好的方法来传递非平凡的对象作为参数?