我有一個ListBox
在我的wpf窗口綁定到ObervableCollection
。我想打開瀏覽器,如果有人點擊ListBox
的元素(就像鏈接一樣)。有人能告訴我如何做到這一點?我用listboxviews發現了一些東西,它只能用這種方式工作,還是有辦法通過使用ListBox
?雙擊列表框項目打開瀏覽器
此致
塞巴斯蒂安
我有一個ListBox
在我的wpf窗口綁定到ObervableCollection
。我想打開瀏覽器,如果有人點擊ListBox
的元素(就像鏈接一樣)。有人能告訴我如何做到這一點?我用listboxviews發現了一些東西,它只能用這種方式工作,還是有辦法通過使用ListBox
?雙擊列表框項目打開瀏覽器
此致
塞巴斯蒂安
您可以將樣式添加到ListBox.ItemContainerStyle,並添加EventSetter有:
<ListBox>
....
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
ListBoxItem_MouseDoubleClick是你與MouseDoubleClick正確的簽名後面的代碼的方法。
我想解決這個問題,而不需要處理代碼隱藏中的listBoxItem雙擊事件,並且我不希望重寫listBoxItem樣式(或者定義要在第一個位置重寫的樣式)。我想在listBox被雙擊時激發一個命令。
我創建了一個附加屬性,像這樣(代碼是非常具體的,但你可以概括它爲必填項):
public class ControlItemDoubleClick : DependencyObject {
public ControlItemDoubleClick()
{
}
public static readonly DependencyProperty ItemsDoubleClickProperty =
DependencyProperty.RegisterAttached("ItemsDoubleClick",
typeof(bool), typeof(Binding));
public static void SetItemsDoubleClick(ItemsControl element, bool value)
{
element.SetValue(ItemsDoubleClickProperty, value);
if (value)
{
element.PreviewMouseDoubleClick += new MouseButtonEventHandler(element_PreviewMouseDoubleClick);
}
}
static void element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ItemsControl control = sender as ItemsControl;
foreach (InputBinding b in control.InputBindings)
{
if (!(b is MouseBinding))
{
continue;
}
if (b.Gesture != null
&& b.Gesture is MouseGesture
&& ((MouseGesture)b.Gesture).MouseAction == MouseAction.LeftDoubleClick
&& b.Command.CanExecute(null))
{
b.Command.Execute(null);
e.Handled = true;
}
}
}
public static bool GetItemsDoubleClick(ItemsControl element)
{
return (bool)element.GetValue(ItemsDoubleClickProperty);
}
}
然後我宣佈我的列表框與附加屬性和我目標命令:
<ListBox ItemsSource="{Binding SomeItems}"
myStuff:ControlItemDoubleClick.ItemsDoubleClick="true">
<ListBox.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="MyCommand"/>
</ListBox.InputBindings>
</ListBox>
希望這會有所幫助。
我用來表達SDK 4.0
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick" SourceName="CaravanasListBox">
<i:InvokeCommandAction Command="{Binding AccionesToolbarCommand}" CommandParameter="{x:Static local:OpcionesBarra.MostrarDetalle}" />
</i:EventTrigger>
</i:Interaction.Triggers>
Jaimir G.
我已經爲了解決這一問題與射擊執行命令更新的安卓解決方案,如果雙擊列表框中的任意位置:
public class ControlDoubleClick : DependencyObject
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ControlDoubleClick), new PropertyMetadata(OnChangedCommand));
public static ICommand GetCommand(Control target)
{
return (ICommand)target.GetValue(CommandProperty);
}
public static void SetCommand(Control target, ICommand value)
{
target.SetValue(CommandProperty, value);
}
private static void OnChangedCommand(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Control control = d as Control;
control.PreviewMouseDoubleClick += new MouseButtonEventHandler(Element_PreviewMouseDoubleClick);
}
private static void Element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Control control = sender as Control;
ICommand command = GetCommand(control);
if (command.CanExecute(null))
{
command.Execute(null);
e.Handled = true;
}
}
}
而在XAML聲明爲ListBox是:
<ListBox ItemsSource="{Binding MyItemsSource, Mode=OneWay}">
<ListBox.ItemContainerStyle>
<Style>
<Setter Property="behaviours:ControlDoubleClick.Command" Value="{Binding DataContext.MyCommand,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
謝謝。這也解決了我的問題。一個後續:這在運行的應用程序中很好用,但是它可以幫助視覺工作室設計師(不是一個表演者,而是一個刺激)。我沒有在應用程序資源中明確定義ListBoxItem樣式,因此BasedOn在設計時會失敗。但是,我有一個在運行時定義此資源的主題庫(WPFTheme)。如果我確定了一個靜態資源,它將消除動態的,主題化的資源。任何想法,我怎樣才能讓兩人一起玩得很好? – el2iot2 2009-05-17 18:54:26
經過多次實驗後,我發現它......我只是在mergedresource屬性中引用主題的xaml: –
el2iot2
2009-05-17 23:56:15
This非常酷,但是如果你已經在ResourceDictionary中定義了ItemContainerStyle呢? – Oliver 2010-04-09 15:54:35