我有一个TextBox,其样式有一个DataTrigger,用于更改文本,如下所示:
<Grid>
<TextBlock Text="Foo">
<TextBlock.Style>
<Style BasedOn="{StaticResource TextStyle}" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding MyBool}" Value="True">
<Setter Property="Text" Value="Bar"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
Run Code Online (Sandbox Code Playgroud)
但它不起作用,文本永远不会变为"Bar".我已经使用Text ="{Binding MyBool}"测试了另一个TextBlock,此文本从"False"变为"True".Snoop没有发现我能看到的错误,输出中没有任何内容.
这个问题可能看起来像WPF触发器绑定到MVVM属性的重复,但我的代码似乎与接受的答案不同(http://www.thejoyofcode.com/Help_Why_cant_I_use_DataTriggers_with_controls_in_WPF.aspx,"使用样式"一节)任何相关的方式.并且在实际答案中建议使用DataTemplate似乎是错误的,因为我只希望将其应用于单个TextBlock,但如果它是正确的,我不知道如何为此编写DataTemplate ...
编辑:
这就是我绑定的属性看起来像:
public bool MyBool
{
get { return _myBool; }
set
{
if (_myBool== value)
return;
_myBool= value;
NotifyPropertyChanged();
}
}
private bool _myBool;
Run Code Online (Sandbox Code Playgroud) 我想启动一个侦听事件并在后台处理它们的线程.这是我到目前为止所做的:
private Thread mThread;
private bool mKeepHandlingIt;
private void Init()
{
mKeepHandlingIt = true;
mThread = new Thread(ProcessEvents);
mThread.SetApartmentState(ApartmentState.STA);
mThread.Start();
}
private void ProcessEvents()
{
StaticClass.CoolEvent += HandleIt;
StaticClass.StartDoingStuff();
while (mKeepHandlingIt)
{
Application.DoEvents();
}
StaticClass.StopDoingStuff();
StaticClass.CoolEvent -= HandleIt;
}
private void HandleIt(object sender, EventArgs e)
{
//Handles it
}
protected override void Dispose()
{
if (mThread != null)
{
mKeepHandlingIt = false;
mThread.Join();
}
}
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?就像一个更适合这个目的的线程类?在这种情况下,BackgroundWorker似乎不是更好的选择......
如果这是一个很好的方法,那么我有一个我无法理解的问题.上面的解决方案适用于侦听事件和处理它们,但是当调用Dispose并且mKeepHandlingIt设置为false时,退出while循环(如果我在循环中放置断点,调试器不会中断)但是后面没有代码执行循环并且mThread.Join永远不会返回.基本上......我想知道的是:如何停止线程,然后确保在清理之前不要继续?
我有一个带有ScrollViewer.VerticalScrollBarVisibility = Auto的RichTextBox,这就像我想要的那样工作.但是,当我将鼠标悬停在文档上时,我在整个RichTextBox元素周围得到一个蓝色边框,而我似乎唯一能让它消失的方法是设置IsHitTestVisible = false,但如果我这样做,滚动条也会被禁用...我尝试过的其他事情是IsFocusable = false并触发RichTextBox的样式,但没有任何成功:
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
Run Code Online (Sandbox Code Playgroud)
我的应用程序中的图像与ListBox中显示的问题相同.我有一个ListBox看起来像这样:
<ListBox ItemsSource="{Binding Photos}"
BorderBrush="{x:Null}"
SelectedItem="{Binding SelectedPhoto, Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid IsItemsHost="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Columns="3" Rows="1"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"
Stretch="Uniform"
SnapsToDevicePixels="True"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent"/>
</Style.Resources>
<Setter Property="Foreground" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{StaticResource Brush_Secondary}"/>
<Setter Property="BorderThickness" Value="5"/>
<Setter Property="Margin" Value="5"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource Brush_Primary}"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource Brush_Selected}"/> …
Run Code Online (Sandbox Code Playgroud)