[英]Bind object to DataTrigger in WPF
I am trying create a Grid
in my application whenever a Button
is pressed. 每当尝试按下Button
时,我都试图在我的应用程序中创建一个Grid
。
XAML XAML
<Grid Name="SideBarGrid" HorizontalAlignment="Right">
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Width" Value="0"/>
<Style.Triggers>
<DataTrigger Binding="What do I put Here?" Value="True"> // HERE
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="Width" To="400" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="Width" To="0" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
C# C#
public partial class MoviePanel : Window {
public MoviePanel() {
InitializeComponent();
}
// List to hold movieDetail objects (each movie is stored with an image and title
List<MediaDetail> movies = new List<MediaDetail>();
MediaDetail selectedMovie;
private void SelectMovie_Click(object sender, RoutedEventArgs e)
{
foreach (MediaDetail media in movies)
{
if (media.id == movieID)
{
selectedMovie = media;
selectedMovie.toDisplay = true;
}
}
}
Media Detail 媒体详情
public class MediaDetail
{
public string id { get; set; }
public bool toDisplay { get; set; }
}
I am uncertain as to how I should be binding the selectedMovie
object to the DataTrigger
. 我不确定应如何将selectedMovie
对象绑定到DataTrigger
。 Also should I be declaring a dependency property
? 我还应该声明一个dependency property
吗?
Your question is unclear on what you're trying to do, you write that you want to bind a DataTrigger to the selectedMovie in one hand and bind to the IsPressed property of a Button on the other hand. 您的问题不清楚您要做什么,您写道,您想一方面将DataTrigger绑定到selectedMovie,另一方面想将其绑定到Button的IsPressed属性。
In this case you won't need a DataTrigger, you'll need a simple Trigger: 在这种情况下,您将不需要DataTrigger,而是需要一个简单的Trigger:
<Trigger Property="IsPressed" Value="True" SourceName="YOUR_BUTTON_NAME">
<!--Your storyboard-->
</Trigger>
A DataTrigger is useful when you want to bind a behavior to a Property in code behind (usually in the ViewModel), what you seems to need here is bind to a DP of another control, a simple Trigger will do that. 当您想将行为绑定到背后的代码(通常在ViewModel中)中的Property时,DataTrigger很有用,这里您似乎需要绑定到另一个控件的DP,一个简单的Trigger可以做到这一点。
Don't forget to name your Button. 别忘了给您的Button命名。
Your selectedMovie is not a property, you need one to bind the DataTrigger to it. 您的selectedMovie不是属性,您需要一个属性将DataTrigger绑定到它。
private MediaDetail _selectedMovie;
public MediaDetail SelectedMovie {
get {return _selectedMovie;}
private set {
_selectedMovie = value;
//notifypropertychanged
}
}
To notify the property changed, you'll need to implement INotifyPropertyChanged. 要通知属性已更改,您需要实现INotifyPropertyChanged。 Or turn the SelectedMovie field into a DP. 或将SelectedMovie字段转换为DP。
So your DataTrigger will be: 因此,您的DataTrigger将是:
<DataTrigger Binding="{Binding SelectedMovie, Mode=OneWay}" Value="THE VALUE">
Assuming that the DataContext is your MoviePanel 假设DataContext是您的MoviePanel
Don't forget to set the DataContext 不要忘记设置DataContext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.