[英]Show chosen menu item
HY,
我有一個包含一些菜單項的菜單。 我還有其他各種元素,例如樹視圖和一些控件。 當我打開程序時,菜單中的所有元素均可用。 但是,我要做的第一步是連接到服務器。 因此,直到通過連接菜單項建立連接后,所有其他元素才可用。
然后,如果要為所有主題選擇特殊的樹狀視圖(例如整個項目結構)項目,那么我只想顯示菜單項。 例如,如果我單擊菜單中的樹視圖條目,則應該有特殊的菜單項可用。
是否可以在xaml中完成此操作?
UPDATE1:
MainWindow.xaml
Title="Service Bus Visualizer" Height="680" Width="1200" Name="Root"
<MenuItem Header="_Read File" Name="readFile" Click="MenuItemReadFile" HorizontalAlignment="Right" Width="187" IsEnabled="{Binding Path=DataContext.IsMonitoring, ElementName=Root}">
<MenuItem.Icon>
<Image Source="Icons/Open.ico" Width="16" Height="16" />
</MenuItem.Icon>
</MenuItem>
MainWindow.xaml.cs
public bool IsMonitoring
{
get
{
return isMonitoring;
}
set
{
isMonitoring = value;
RaisePropertyChanged("IsMonitoring");
}
}
private bool isMonitoring;
public MainWindow()
{
InitializeComponent();
this.IsMonitoring = false;
this.DataContext = this;
Application.Current.MainWindow = this;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
ConnectionWindow.xaml.cs
MainWindow mainWindow = Application.Current.MainWindow as MainWindow;
mainWindow.IsMonitoring = true;
我在輸出窗口上沒有收到任何錯誤,但這不起作用?
UPDATE2:
我有第二個參數,它是一個ObservableCollection。
MainWindow.xaml
<ListBox Grid.Row="3" Name="Logger" ItemsSource="{Binding Path=DataContext.LoggingList, ElementName=Root}" DisplayMemberPath="Message" IsSynchronizedWithCurrentItem="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionChanged="BringSelectionIntoView">
</ListBox>
MainWindow.xaml.cs
public static ObservableCollection<Log> LoggingList { get; set; }
public MainWindow()
{
LoggingList = new ThreadSafeObservableCollection<Log>();
this.IsMonitoring = false;
this.DataContext = this;
Application.Current.MainWindow = this;
InitializeComponent();
}
Log.cs
public class Log : INotifyPropertyChanged
{
public string Message {
get
{
return message;
}
set
{
message = value;
NotifyPropertyChanged("Message");
}
}
public string message;
public Log()
{
}
public Log(string message)
{
this.Message = message;
NotifyPropertyChanged("Message");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
最好的祝福
首先,就“可用性”而言,您有兩個選擇。 “ IsEnabled”屬性和“ Visible”屬性。 “ IsEnabled”是一個布爾值,它確定用戶是否可以單擊/選擇/與給定元素進行交互。 一般來說,如果將此屬性設置為false,則該元素將顯示為“灰色”。
更改可見性將使元素完全顯示/消失。 設置為“可見”(實際上是一個枚舉)時,它會正常顯示。 設置為“隱藏”時,其空間保留在UI上,但是您實際上看不到它。 當設置為“ Collapsed”時,您將看不到它,並且在布局中沒有保留空間。
對於您的第一個要求(等待連接到服務器),我將使用IsEnabled綁定到“ IsConnected”屬性,如下所示:
IsEnabled="{Binding IsConnected}"
這將在需要具有此行為的每個項目上進行。
“特定於上下文的”菜單項稍微復雜一些,但是基本思想是為每個上下文相關項(例如:)綁定Visible:
Visible="{Binding Path=SelectedItem, ElementName=MyTreeView, Converter={StaticResource SelectedItemToVisibilityConverter}, ConverterParameter={x:Type ChildItem}"
我假設每個項目的可見性取決於所選的項目類型(子項目或父項目),如果我錯了,您應該可以擴展該示例。 轉換器將如下所示:
public class SelectedItemToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((parameter as Type).IsInstanceOfType(value))
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(...)
{
return Binding.DoNothing;
}
}
請讓我知道是否可以澄清任何事情。 希望這可以為您嘗試做的事情提供一個良好的起點。
更新:
查看您的代碼,我看到一些潛在的問題:
IsMonitoring
被聲明為公共字段。 綁定僅適用於公共屬性。 此屬性需要引發PropertyChanged
事件才能起作用。 this.DataContext = this
使其在構建ViewModel類之前起作用。 IsMonitoring
字段在“ MainWindow.xaml.cs”文件中聲明。 首先,這應該在視圖模型中。 其次,綁定正在MenuItem
類上尋找該屬性(可能是因為它在某種ItemsControl中)。 如果要在根數據上下文上使用它,請為窗口指定一個名稱(例如“ Root”),並使用以下綁定:
"{Binding Path=DataContext.IsMonitoring, ElementName=Root}"
希望這是有道理的。 讓我知道是否可以提供進一步的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.