我希望有一个TextBox以货币格式显示的数字(通过设置 StringFormat=c绑定).当TextBox选择(何时IsKeyboardFocused==true)时,我希望格式化消失,直到焦点TextBox丢失为止.
我找到了一种方法,下面粘贴代码.我的问题是绑定是在Style- 内部指定的- 这意味着我必须重新键入TextBox我想要执行此操作的样式.理想情况下,我想把风格置于中心位置,并为每个人重复使用它,每个都有TextBox不同的绑定目标.
有没有办法让我使用a Style在现有绑定上设置参数,比如Text.Binding.StringFormat=""?(而不是将Text的整个值设置为新定义的Binding)
其他建议也将受到赞赏.
代码(这是有效的,它只是不方便):
<TextBox x:Name="ContractAmountTextBox">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=ContractAmountTextBox}" Value="False">
<Setter Property="Text" Value="{Binding Path=ContractAmount, UpdateSourceTrigger=LostFocus, StringFormat=c}"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=ContractAmountTextBox}" Value="True">
<Setter Property="Text" Value="{Binding Path=ContractAmount, UpdateSourceTrigger=LostFocus}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
Run Code Online (Sandbox Code Playgroud)
在WPF,Prism v4中,我有一个ToolBar作为一个区域.在这个区域中,我注入UserControls,其中包含工具栏的按钮和分隔符.问题是因为按钮现在位于UserControl中,所以工具栏样式不再适用.
我可以通过定义样式来解决这个问题:
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"/>
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"/>
Run Code Online (Sandbox Code Playgroud)
但我想避免这种情况,因为我有数百个按钮.所以我的问题是我是否可以从资源字典中为ToolBar下的所有按钮定义样式?
谢谢!
我的WinForms程序中有一个跟踪栏,通过移动它可以刷新一个庞大而耗时的方法.看看这段代码:
trackBar_ValueChanged(object sender, EventArgs e)
{
this.RefreshData();
}
Run Code Online (Sandbox Code Playgroud)
这条轨道酒吧有20个台阶.如果用户抓住轨迹栏的滑块并在我的情况下从20拉到0,"刷新数据"将执行20次,虽然它在结束"刷新数据"值为0的过程时显示正确的数据,我想做类似的事情它仅在trackBar滑块释放时调用'RefreshData',因此它不会处理轨迹栏上释放点的所有步骤.
任何有关实现这一目标的帮助和提示都将得到满足!谢谢.
我正在玩HttpListener和Async CTP
class HttpServer : IDisposable
{
HttpListener listener;
CancellationTokenSource cts;
public void Start()
{
listener = new HttpListener();
listener.Prefixes.Add("http://+:1288/test/");
listener.Start();
cts = new CancellationTokenSource();
Listen();
}
public void Stop()
{
cts.Cancel();
}
int counter = 0;
private async void Listen()
{
while (!cts.IsCancellationRequested)
{
HttpListenerContext context = await listener.GetContextAsyncTask(); // my extension method with TaskCompletionSource and BeginGetContext
Console.WriteLine("Client connected " + ++counter);
// simulate long network i/o
await TaskEx.Delay(5000, cts.Token);
Console.WriteLine("Response " + counter);
// send response
}
listener.Close(); …Run Code Online (Sandbox Code Playgroud) UIElements 上的DependencyProperties 不支持数据绑定(你得到类似的东西:
"找不到管理FrameworkElement ..")
.如果您尝试,则会收到错误,因为WPF无法解析DataContext.据我所知,如果继承FrameworkElement或Freezable,你将获得绑定支持,但在这种情况下,我不能简单地更改基类.有没有办法让UIElement支持数据绑定?
我试图将DataContext属性添加到UIElement类,如下所示:
FrameworkElement.DataContextProperty.AddOwner(typeof(Bitmap), new
FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits));
Run Code Online (Sandbox Code Playgroud)
我还尝试通过在绑定表达式中指定"ElementName"来绑定,但我仍然无法解析父DataContext(我认为ElementName显式绑定只会消除解析DataContext的需要).
这是绑定.有问题的类称为"位图".
<Utils:Bitmap Source="{Binding Path=Icon}" />
<TextBlock Grid.Row="1" Grid.ColumnSpan="3" MaxWidth="90" Text="{Binding Path=Name}" TextWrapping="Wrap" TextAlignment="Center"/>
Run Code Online (Sandbox Code Playgroud)
文本块绑定按预期工作,第一个绑定不工作.绑定的viewmodel具有两个属性(我之前绑定到Image类并且它工作).
位图类可以在这个博客上找到:http://blogs.msdn.com/b/dwayneneed/archive/2007/10/05/blurry-bitmaps.aspx
通过一些扩展的绑定诊断,我得到了这个输出:
System.Windows.Data Warning: 65 : BindingExpression (hash=14926099): Framework mentor not found
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Icon; DataItem=null; target element is 'Bitmap' (HashCode=117163); target property is 'Source' (type 'BitmapSource')
System.Windows.Data Warning: 63 : BindingExpression (hash=6195855): Resolving source (last chance)
System.Windows.Data Warning: …Run Code Online (Sandbox Code Playgroud) 我Mouse_Event在Delphi 2009 中使用了这个函数,但是Delphi的文档说这个函数已被取代并SendInput代替使用.
Delphi SendInput文档定义了语法和参数,但没有示例,也不清楚如何使用该函数.我在网上浏览过,找不到任何好的Delphi示例.
具体来说,我试图模拟左鼠标然后向上.目前我这样做Mouse_Event如下:
Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
我该如何使用SendInput?
跟进:
我最终留下了我的代码,就像@David建议的那样.
但我给了@ opc0de答案,因为他确实回答了我的问题.但是,我不能保证它是正确的,因为我从来没有尝试过.
<DataTemplate x:Key="MyTemplate" DataType="{x:Type l:MyViewModel}">
<l:MyView />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
有没有什么办法可以用我的一类价值取代"MyTemplate"?
public sealed class MyTemplateSelector : DataTemplateSelector
{
public const string TemplateName = "MyTemplate";
//I use the TemplateName const to retrieve the correct DataTemplate
}
Run Code Online (Sandbox Code Playgroud) background当点击另一个按钮时,我尝试更改一个按钮.
如果我提供按钮样式,我不能这样做.
请参阅下面的代码.
MainWindow.xaml
<Window x:Class="WpfApplication1.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">
<Window.Resources>
<Style TargetType="Button" x:Key="TransparentButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="2,2,2,2" HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="borderTemplate" Property="Border.BorderBrush" Value="Gray" />
<Setter TargetName="borderTemplate" Property="Border.BorderThickness" Value="1" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="borderTemplate" Property="Border.BorderBrush" Value="Lime" />
</Trigger>
<Trigger Property="IsFocused" Value="true">
<Setter TargetName="borderTemplate" Property="Border.Background" Value="#FD7" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="borderTemplate" Property="Border.Background" Value="LightGray"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button …Run Code Online (Sandbox Code Playgroud) 我有一个带有命令绑定和使用Prism库的文本块.
这是XAML的一部分:
<TextBlock Margin="0,10,0,0">SocialSecurityNumer:</TextBlock>
<TextBox Name="SSNText" GotFocus="TextBox_GotFocus" Text="{Binding SSN, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0"/>
Run Code Online (Sandbox Code Playgroud)
这是后面的ViewModel:
public FindViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
FindCommand = new DelegateCommand(
() => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
() => !string.IsNullOrWhiteSpace(Kennitala)
);
}
public DelegateCommand FindCommand { get; set; }
private string ssn;
public string SSN
{
get { return ssn; }
set
{
if (ssn== value)
return;
ssn = value;
RaisePropertyChanged(() => SSN);
FindCommand.RaiseCanExecuteChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
这是GridViewModel,它监听此事件触发器并使用SSN作为参数启动一个函数
public class GridViewModel : NotificationObject
{
public GridViewModel()
{
var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
eventAggregator.GetEvent<SSNChangedEvent>().Subscribe(GetData); …Run Code Online (Sandbox Code Playgroud) wpf ×7
c# ×6
styles ×4
xaml ×4
data-binding ×2
mouseevent ×2
asynchronous ×1
button ×1
datacontext ×1
delphi ×1
key ×1
mouse ×1
prism ×1
resources ×1
sendinput ×1
trackbar ×1
uielement ×1
winforms ×1