English 中文(简体)
WPF ContextMenu oblig MenuItem Icon to MenuItem Header
原标题:WPF ContextMenu bind MenuItem Icon to MenuItem Header

请解释为什么这样做不可行。

 <ContextMenu>     
      <MenuItem>
          <MenuItem.Header>
               <TextBox Name="tbColor" Text="Black" />
          </MenuItem.Header>
          <MenuItem.Icon>
               <TextBox Text="{Binding ElementName=tbColor,Path=Text}" />
          </MenuItem.Icon>                                                                    
      </MenuItem>
 </ContextMenu>

I have tried several ways but nothing. RelativeSource doesn t work..

Edit. binding from another control work.

<DataGrid Foreground="{Binding ElementName=tbColor,Path=Text,Converter={StaticResource textToBrushConverter}}">          
        <DataGrid.ContextMenu>
            <ContextMenu>....             
最佳回答

最初的法典就是这样,只是为了我自己的先辈。

<MenuItem.Header>
   <TextBox Name="tbColor" Text="Black" TextChanged="tbColor_TextChanged" />
</MenuItem.Header>
<MenuItem.Icon>
   <Rectangle Name="rectangleColor" Width="20" Height="20" />
</MenuItem.Icon>

And in code behind..

private void tbColor_TextChanged(object sender, TextChangedEventArgs e)
    {
        try
        {
            rectangleColor.Fill = new SolidColorBrush((Color) ColorConverter.ConvertFromString(((TextBox) sender).Text));
        }
        catch (Exception)
        {
            return;
        }
    }
问题回答

Header a MenuItem 的财产实际上正在寻找一种扼杀。 可在<代码>Header内安装<代码>TextBlock,但不得在上安装<代码>。

尽管我已经深入审视了这个问题,但我怀疑,<代码>I上的同样(除图像外)情况是<代码>。

Context menu outside the visual tree of your control so can t find element name. Try -

{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签