0
나는이 방법을 시도했다. 그래서 button1의 menuitem을 클릭하면 두 번째 버튼에서 동일한 menuitem을 확인하고 체크한다. 그러나 오류가 발생하는 부분은 ... 더 좋은 방법으로 쓰고 싶다. 그것은 내가 issuse.here 밖으로 필요한대로 작동합니다 그래서 내가 지금까지 한 코드는 ... 어떻게이 문제를 해결할 수뿐만 아니라 내가이 코딩을 향상시킬 수 있습니다 ... 여기 내 코드입니다. XAML 뒤에.항상 오류 발생 - "개체 참조가 개체의 인스턴스로 설정되지 않았습니다."
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" FlowDirection="LeftToRight">
<Button Name="btnPhone" Content="Business" Click="OnbtnPhoneClick" ></Button>
<Button Name="ddBtnPhone" Width="25" Click="OnddBtnPhoneClick" Height="47">
<Button.Content>
<Path x:Name="btnArrow1" Margin="4" VerticalAlignment="Center" Width="10" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
</Button.Content>
<Button.ContextMenu>
<ContextMenu x:Name="cmPhone">
<MenuItem Header="Assistant " Name="mnIAssistant" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/> <MenuItem Header="Business" Name="mnIBusiness" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/> <MenuItem Header="Business 2 " Name="mnIBusiness2" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Business FAX " Name="mnIBusinessFAX" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Call Back" Name="mnICallback" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Car" Name="mnICar" Click="OnMenuItemChecked"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
<TextBox Height="19" Name="textBox1" Width="120" />
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" FlowDirection="LeftToRight">
<Button Name="btnHome" Content="Home" Click="OnbtnHomeClick" Height="26" Width="64" />
<Button Name="ddBtnHome" Width="25" Click="OnddBtnHomeClick" Margin="0,0,0,5" >
<Button.Content>
<Path x:Name="btnArrow2" Margin="4" VerticalAlignment="Center" Width="10" Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
</Button.Content>
<Button.ContextMenu>
<ContextMenu Name="cmHome">
<MenuItem Header="Assistant " Name="mnIAssistant1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Business" Name="mnIBusiness1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Business 2 " Name="mnIBusiness21" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Business FAX " Name="mnIBusinessFAX1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Call Back" Name="mnICallback1" Checked="OnMenuItemChecked" Click="OnMenuItemClick"/>
<MenuItem Header="Car" Name="mnICar1" Click="OnMenuItemChecked"/>
</ContextMenu>
</Button.ContextMenu>
</Button>
<TextBox Height="23" Name="textBox2" Width="120" />
</StackPanel>
<Border Grid.Row="3" BorderThickness="2" BorderBrush="Blue">
<TextBlock Name="tbWebpage" Grid.Row="3" Background="White" VerticalAlignment="Top" Height="20">
<Hyperlink NavigateUri="tbWebpage.Text" RequestNavigate="Hyperlink_RequestNavigate"></Hyperlink>
</TextBlock>
</Border>
</Grid>
코드
namespace MenuItemsChecked
{
public partial class MainWindow : Window
{
//string[] arrMenitems = { "business", "a1", "a2", "a3","a4" };
Button btnCommon = new Button();
TextBox txtCommon = new TextBox();
MenuItem mnItem;
public MainWindow()
{
InitializeComponent();
}
private void AddMenuItems(ContextMenu cMenu, Button btnName, TextBox txtBoxName)
{
//mnItem.Items.Add(arrMenitems);
if (cMenu.Items.Count != 0)
{
//mnItem = new MenuItem();
//mnItem.Header = "Business";
//mnItem.IsCheckable = true;
//cMenu.Items.Add(mnItem);
//mnItem = new MenuItem();
//mnItem.Header = "Assistant";
//mnItem.IsCheckable = true;
//cMenu.Items.Add(mnItem);
//mnItem = new MenuItem();
//mnItem.Header = "Business 2";
//mnItem.IsCheckable = true;
//cMenu.Items.Add(mnItem);
//mnItem = new MenuItem();
//mnItem.Header = "Business FAX";
//mnItem.IsCheckable = true;
//cMenu.Items.Add(mnItem);
//mnItem = new MenuItem();
//mnItem.Header = "Call Back";
//mnItem.IsCheckable = true;
//cMenu.Items.Add(mnItem);
//cMenu.Items.Add(mnItem);
//cmCommon = cMenu;
cMenu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
cMenu.AddHandler(MenuItem.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseButtonUpEventClick));
// cMenu.AddHandler(MenuItem.CheckedEvent, new RoutedEventHandler(OnMenuItemChecked));
btnCommon = btnName;
//txtCommon = txtBoxName;
}
}
private void OnMenuItemChecked(object sender, RoutedEventArgs e)
{
RoutedEventArgs args = e as RoutedEventArgs;
MenuItem item = args.OriginalSource as MenuItem;
if (sender == mnIAssistant)
{
mnIAssistant.IsChecked = true;
mnIAssistant1.IsChecked = true;
}
if (sender == mnIBusiness)
{
mnIBusiness.IsChecked = true;
mnIBusiness1.IsChecked = true;
}
if (sender == mnIBusiness2)
{
mnIBusiness2.IsChecked = true;
mnIBusiness21.IsChecked = true;
}
if (sender == mnIBusinessFAX)
{
mnIBusinessFAX.IsChecked = true;
mnIBusinessFAX1.IsChecked = true;
}
if (sender == mnICallback)
{
mnICallback.IsChecked = true;
mnICallback1.IsChecked = true;
}
if (sender == mnICar)
{
mnICar.IsChecked = true;
mnICar1.IsChecked = true;
}
if (sender == mnIAssistant1)
{
mnIAssistant.IsChecked = true;
mnIAssistant1.IsChecked = true;
}
if (sender == mnIBusiness1)
{
mnIBusiness.IsChecked = true;
mnIBusiness1.IsChecked = true;
}
if (sender == mnIBusiness21)
{
mnIBusiness2.IsChecked = true;
mnIBusiness21.IsChecked = true;
}
if (sender == mnIBusinessFAX1)
{
mnIBusinessFAX.IsChecked = true;
mnIBusinessFAX1.IsChecked = true;
}
if (sender == mnICallback1)
{
mnICallback.IsChecked = true;
mnICallback1.IsChecked = true;
}
if (sender == mnICar1)
{
mnICar.IsChecked = true;
mnICar1.IsChecked = true;
}
}
private void OnMouseButtonUpEventClick(object sender, MouseButtonEventArgs e)
{
(sender as MenuItem).IsChecked = !(sender as MenuItem).IsChecked;
((sender as MenuItem).Parent as ContextMenu).IsOpen = false;
}
private void OnMenuItemClick(object sender, RoutedEventArgs e)
{
RoutedEventArgs args = e as RoutedEventArgs;
// MenuItem item = args.OriginalSource as MenuItem;
//string header = item.Header.ToString();
MenuItem menuitem = sender as MenuItem; ///always getting error-"Object reference not set to an instance of an object."
//ContextMenu cm = sender as ContextMenu;
string name = menuitem.Name;
string header = menuitem.Header.ToString();
if ((sender == mnIBusiness) |(sender == mnIBusiness1))
{
if (header == "Business")
{
mnIBusiness.IsChecked = true;
mnIBusiness1.IsChecked = true;
}
btnCommon.Content = header;
//item.IsCheckable = true;
}
else if ((sender == mnIAssistant) | (sender == mnIAssistant1))
{
if (header == "Assistant")
{
mnIAssistant.IsChecked = true;
mnIAssistant1.IsChecked = true;
}
btnCommon.Content = header;
///item.IsChecked = true;
}
else if ((sender == mnIBusiness2) | (sender == mnIBusiness21))
{
//if(header=="Business 2")
//{
mnIBusiness2.IsChecked = true;
mnIBusiness21.IsChecked = true;
//}
btnCommon.Content = header;
//item.IsChecked = true;
}
else if ((sender == mnIBusinessFAX) |(sender == mnIBusinessFAX1))
{
if(header=="Business FAX")
{
mnIBusinessFAX.IsChecked = true;
mnIBusinessFAX1.IsChecked = true;
}
btnCommon.Content = header;
//item.IsChecked = true;
}
else if ((sender == mnICallback1) | (sender == mnICallback))
{
if (header == "Call Back")
{
mnICallback.IsChecked = true;
mnICallback1.IsChecked = true;
}
btnCommon.Content = header;
//item.IsChecked = true;
}
else
{
if (header == "Car")
{
mnICar.IsChecked = true;
mnICar1.IsChecked = true;
}
btnCommon.Content = header;
}
txtCommon.Text = string.Empty;
}
private void OnddBtnPhoneClick(object sender, RoutedEventArgs e)
{
ButtonContextMenuDisplay(sender);
AddMenuItems(cmPhone, btnPhone, textBox1);
}
private void OnbtnPhoneClick(object sender, RoutedEventArgs e)
{
}
private void OnbtnHomeClick(object sender, RoutedEventArgs e)
{
}
private void OnddBtnHomeClick(object sender, RoutedEventArgs e)
{
ButtonContextMenuDisplay(sender);
AddMenuItems(cmHome, btnHome, textBox2);
}
private void ButtonContextMenuDisplay(object sender)
{
(sender as Button).ContextMenu.IsEnabled = true;
(sender as Button).ContextMenu.PlacementTarget = (sender as Button);
(sender as Button).ContextMenu.IsOpen = true;
(sender as Button).ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
}
}
}
KIDA는 문제가 해결 받고 있습니다 헤이 .. – loop
아니, 아직 ..., 난 여전히 같은 예외가 – kida
이 문제는 널 참조로 인한 것입니다. 그래서 당신은 이것을 얻고 있습니다. – loop